@mschluepmann, But set UIKeyboardTypeASCIICapable can not input Chinese
And you can do it like below
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (IS_OS_7_OR_LATER) {
if ([textField isFirstResponder]) {
if ([[[textField textInputMode] primaryLanguage] isEqualToString:@"emoji"] || ![[textField textInputMode] primaryLanguage]) { // In fact, in iOS7, '[[textField textInputMode] primaryLanguage]' is nil
return NO;
}
}
} else {
if ([[[UITextInputMode currentInputMode] primaryLanguage] isEqualToString:@"emoji"] ) {
return NO;
}
}
return YES;
}
But sometimes, the emoji may not entered by emoji keyboard. For example, when you type "哈哈" it shows emoji on the header of the keyboard. In the case, the code above will make no effect. So you should do a twice validation as following:
- (BOOL)isValidString
{
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[^\\u0020-\\u007E\\u00A0-\\u00BE\\u2E80-\\uA4CF\\uF900-\\uFAFF\\uFE30-\\uFE4F\\uFF00-\\uFFEF\\u0080-\\u009F\\u2000-\\u201f\r\n]" options:NSRegularExpressionCaseInsensitive error:nil];
NSUInteger numberOfMatches = [regex numberOfMatchesInString:self options:NSMatchingWithTransparentBounds range:NSMakeRange(0, [self length])];
if (numberOfMatches > 0) {
return NO;
}
return YES;
}