How to disable iOS 8 emoji keyboard?

前端 未结 12 2166
说谎
说谎 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

    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
    }
    

提交回复
热议问题