How to hide emojis in keyboard and ignore emoji character for textField in Swift

ODENZA
Feb 2, 2023

--

In this article, I’, going to show you how to set your keyboard(hide emojis) for textField.

Your textField:

@IBOutlet weak var yourTextField: UITextField!

Set keyboard in ViewDidLoad()

yourTextField.keyboardType = .asciiCapable

Result:

But for yourTextField, Users can copy emoji and paste it to your textField.

And I’m going to prevent it. I will use yourTextField.delegate = self and

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let isContainEmoji = string.unicodeScalars.filter({ $0.properties.isEmoji }).count > 0
let numberCharacters = string.rangeOfCharacter(from: .decimalDigits)

if isContainEmoji && numberCharacters == nil {
return false
}
}

Now, I can ignore emojis for myTextField.

--

--