I want my UITextField to only display English characters, numbers, and whitespace. However with the Vietnamese keyboard, when input “W” or “OW” the text field will display as “Ư” or “Ơ” which my function below cannot detected. I try to implement even when using Vietnamese keyboard, the text field shows English characters.
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText string: String) -> Bool {
guard let text = self.text else { return false }
let allowedCharacters = CharacterSet(charactersIn: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ")
let characterSet = CharacterSet(charactersIn: string)
let newString = (text as NSString).replacingCharacters(in: range, with: string)
switch charCountState {
case .counter(let max):
return newString.count <= max && allowedCharacters.isSuperset(of: characterSet)
case .none:
return true
}
}