Is there any option to stop showing the emoji keyboard in iOS 8? It is not available in numpad and secure text but for email it is there. If it is not possible to disable i
The solution that worked for me :
self.texField.keyboardType = UIKeyboardTypeASCIICapable;
The emoji keyboard won't be available with that KeyboardType.
But it's compulsory to check the kind of every char added in the textField, because the user can copy an emoji from elsewhere then paste it in.
Here is how you could do so :
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
return [text canBeConvertedToEncoding:NSASCIIStringEncoding];
}
Be warned that, as I read in another post, this might also prevent user from using keyboard with special characters, like the Chinese one.
Though The Question is super old, I was Facing the same problem and was able to resolve it by the time this page loaded by this small trick :
Simply Select "ASCII Capable" or "Numbers and Punctuations" in Interface Builder Xcode 8.2.1
Output is No Emoji Keyboard =D
I'm sure it'll help Someone =)
I wanted an answer for Swift 3.0 that would work on shouldChangeTextInRange
and came up with the following that is really simple and can handle any language you throw at it (I include a photo using Chinese to demonstrate).
The guard statement unwraps the optional text, and the return statement checks to see if it pulled out a value that satisfied the trimmingCharacters
check. If there is a value (meaning it is not part of the current devices's input language), then it will not be empty, so the method returns false and does not allow the character to be entered.
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
guard let text = (textField.text as NSString?)?.replacingCharacters(in: range, with: string) else {
return true
}
return text.trimmingCharacters(in: CharacterSet.alphanumerics).isEmpty
}
This relies on Apple's CharacterSet.alphanumerics
, so there is no need to maintain and update a list of approved characters, unless you want to add or remove items.
Yay for language agnostic!
Another common use is for email, since the default email keyboard provides emojis. I found with a small modification, I was able to restrict characters to what I wanted for an email address to work.
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
guard let text = (textField.text as NSString?)?.replacingCharacters(in: range, with: string) else {
return true
}
return text.trimmingCharacters(in: CharacterSet.alphanumerics).isEmpty ||
!text.characters.filter { $0 == "@" || $0 == "." }.isEmpty
}
For those who require international keyboards, this has worked well for iOS 9 and above:
Add the following extension function to check if a string contains emoji:
extension String {
var containsEmoji: Bool {
for scalar in unicodeScalars {
switch scalar.value {
case 0x3030, 0x00AE, 0x00A9,// Special Characters
0x1D000...0x1F77F, // Emoticons
0x2100...0x27BF, // Misc symbols and Dingbats
0xFE00...0xFE0F, // Variation Selectors
0x1F900...0x1F9FF: // Supplemental Symbols and Pictographs
return true
default:
continue
}
}
return false
}
}
Another extension function to remove the emoji's from the string:
extension String {
var minusEmojis: String {
let emojiRanges = [0x1F601...0x1F64F, 0x2702...0x27B0, 0x1D000...0x1F77F, 0x2100...0x27BF, 0xFE00...0xFE0F, 0x1F900...0x1F9FF]
let emojiSet = Set(emojiRanges.joined())
return String(self.filter { !emojiSet.contains($0.unicodeScalarCodePoint) })
}
}
Add a target to your text field like this:
textField.addTarget(self, action: #selector(didChangeText), for: .editingChanged)
Finally remove any emoji present from the target selector:
@objc func didChangeText(field: UITextField) {
if (field.text?.containsEmoji == true) {
field.text = field.text?.minusEmojis
}
}
Hope this helps someone! Cheers.
You can try this (Swift 5).
Create String extension:
extension String {
func containsEmoji() -> Bool {
for scalar in unicodeScalars {
if !scalar.properties.isEmoji { continue }
return true
}
return false
}
}
Then use it in UITextFieldDelegate like this:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if string.containsEmoji() { return false }
return true
}
In Swift : - It Will restrict the to past the emoji in your TextView and Just change the keyboard type to ascii capable
func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
var result = Bool()
let disallowedCharacterSet1 = NSCharacterSet.symbolCharacterSet()
let replacementStringIsLegal = text.rangeOfCharacterFromSet(disallowedCharacterSet1) == nil
result = replacementStringIsLegal
let newLength = textView.text!.utf16.count + text.utf16.count - range.length
return newLength <= 300 && result // Bool
}