解放代理,解放小键盘,从根本解决问题
[textField addTarget:self action:@selector(textChange:) forControlEvents:UIControlEventEditingChanged];
- (void)textChange:(UITextField*)textField {
static const int Max_Text_Length = 5;
//获取当前键盘类型
UITextInputMode *mode = (UITextInputMode *)[UITextInputMode activeInputModes][0];
//获取当前键盘语言
NSString *lang = mode.primaryLanguage;
if ([lang isEqualToString:@"zh-Hans"]) {//如果语言是汉语(拼音)
//取到高亮部分范围
UITextRange *selectedRange = [textField markedTextRange];
//取到高亮部分
UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
//如果取不到高亮部分,代表没有拼音
if (!position){//当期超过最大限制时
if (textField.text.length > Max_Text_Length) {//对超出部分进行裁剪
textField.text = [textField.text substringToIndex:Max_Text_Length];
}
}
} else {//如果语言不是汉语,直接计算
if (textField.text.length > Max_Text_Length) {
textField.text = [textField.text substringToIndex:Max_Text_Length];
}
}
}