45

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 it how to get the string values from the emoji?

1

12 Answers 12

120

Try this:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{        
    if ([textField isFirstResponder])
    {
        if ([[[textField textInputMode] primaryLanguage] isEqualToString:@"emoji"] || ![[textField textInputMode] primaryLanguage])
        {
            return NO;
        }
    }
    return YES;
}

for more info see here.

EDIT :

You can hide emoji from Keyboard using this code:

txtField.keyboardType=UIKeyboardTypeASCIICapable;

EDIT :

Hide emoji selection from keyboard using Interface builder.

enter image description here

5
  • 3
    Another Hint: You can set this directly from Xcode/Interface Builder.
    – BastiBen
    Nov 28, 2014 at 15:55
  • 2
    If emoji is copied from another textfield this check should be done if just ASCII characters accepted string.canBeConvertedToEncoding(NSASCIIStringEncoding), or if just emoji should be removed this lib can be used github.com/woxtu/NSString-RemoveEmoji Nov 23, 2015 at 10:38
  • 5
    It should be noted that changing the keyboard type to ASCII Capable will also remove access other international keyboards. Nov 16, 2016 at 23:27
  • 1
    Well answered! :)
    – Goppinath
    Jul 11, 2017 at 6:14
  • it works on iOS 13 for me but not for Password isSecureTextEntry = true text field Nov 12, 2019 at 14:03
19

This works on iOS 7 and 8:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    // Disable emoji input
    return [textField textInputMode] != nil;
}
1
  • The only thing that actually works without deprecated methods!
    – hris.to
    Mar 23, 2015 at 15:23
13

Just to update the great answers above for anyone facing the same issues as me:

  1. An email field set as keyboardType = .EmailAddress still allows emojis
  2. You may have more than one condition you want to filter
  3. Swift
  4. Prevent C&P

This is how we did it:

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    if !string.canBeConvertedToEncoding(NSASCIIStringEncoding){
        return false
    }
    //other conditons
}

Edit

Okay the solution with NSASCIIStringEncoding is no good if you want to allow certain characters for other languages.

Some letters blocked with NSASCIIStringEncoding:

  • All other versions of a: àáâäæãåā
  • All other versions of e: èéêëēėę
  • All other versions of o: ôöòóœøōõ

Using NSASCIIStringEncoding is not a good idea if you want to support users from other countries!

I also tried NSISOLatin1StringEncoding:

  • Blocked versions of a: ā
  • Blocked versions of e: ēėę
  • Blocked versions of o: œø

This is much better clearly.

I also tried NSISOLatin2StringEncoding:

  • Blocked versions of a: àæãåā
  • Blocked versions of e: èêēė
  • Blocked versions of o: òœøōõ

Worse.

Edit2:

The following seems to work:

extension String {
    func containsEmoji() -> Bool {
        for i in self.characters {
            if i.isEmoji() {
                return true
            }
        }
        return false
    }
}

extension Character {
    func isEmoji() -> Bool {
        return Character(UnicodeScalar(0x1d000)) <= self && self <= Character(UnicodeScalar(0x1f77f))
            || Character(UnicodeScalar(0x1f900)) <= self && self <= Character(UnicodeScalar(0x1f9ff))
            || Character(UnicodeScalar(0x2100)) <= self && self <= Character(UnicodeScalar(0x26ff))
    }
}

Now we call:

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    if string.containsEmoji() {
        return false
    }
    //other conditons
}
11

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.

1
  • 1
    this should be the accepted answer since people can still copy/paste emoji's with the accepted answer above May 6, 2016 at 23:57
9

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

enter image description here enter image description here

Output is No Emoji Keyboard =D

enter image description here

I'm sure it'll help Someone =)

1
  • 1
    Please don't add the same answer to multiple questions. Answer the best one and flag the rest as duplicates, once you earn enough reputation. If it is not a duplicate, tailor the post to the question and flag for undeletion. Jun 6, 2017 at 16:20
8

textField.keyboardType = .asciiCapable

4

Swift 3.1 Solution

// MARK: - Text Field Delegate

 func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        return textField.textInputMode != nil
    }
1
4

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
}
3
3

This is swift version.

func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
    if textView.isFirstResponder() {
       if textView.textInputMode?.primaryLanguage == nil || textView.textInputMode?.primaryLanguage == "emoji" {
            return false;
        }
    }
    return true;
}
3

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.

enter image description here

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
}
2

For those who require international keyboards, this has worked well for iOS 9 and above:

  1. 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
       }
    }
    
  2. 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) })
       }
    }
    
  3. Add a target to your text field like this:

    textField.addTarget(self, action: #selector(didChangeText), for: .editingChanged)
    
  4. 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.

0

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

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.