How to disable iOS 8 emoji keyboard?

前端 未结 12 2144
说谎
说谎 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:19

    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
    }
    

提交回复
热议问题