How to disable iOS 8 emoji keyboard?

前端 未结 12 2119
说谎
说谎 2020-12-03 00:33

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

相关标签:
12条回答
  • 2020-12-03 01:20

    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

    0 讨论(0)
  • 2020-12-03 01:20

    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
    }
    
    0 讨论(0)
  • 2020-12-03 01:25

    In swift 3.0

    textField.keyboardType = .asciiCapable

    0 讨论(0)
  • 2020-12-03 01:25

    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;
    }
    
    0 讨论(0)
  • 2020-12-03 01:33

    This works on iOS 7 and 8:

    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
    {
        // Disable emoji input
        return [textField textInputMode] != nil;
    }
    
    0 讨论(0)
  • 2020-12-03 01:33

    Swift 3.1 Solution

    // MARK: - Text Field Delegate
    
     func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
            return textField.textInputMode != nil
        }
    
    0 讨论(0)
提交回复
热议问题