1.给textField增加一个事件:
[_firstTextField addTarget:self action:@selector(inputTextFieldChangeValue:) forControlEvents:UIControlEventEditingChanged];
2.在textFieldDidChange中,就可以接收到,所有的输入,但是需要将选中态时的拼音排除掉。
- (void)inputTextFieldChangeValue:(UITextField *)textField {
/**< 当输入中文时,只获取中文部分,过滤掉多余的字母 */
UITextRange *selectedRange = [textField markedTextRange];
NSString * newText = [textField textInRange:selectedRange];
if (newText.length > 0) {/**< 获取高亮部分 */
return;
}
NSMutableString *string = [NSMutableString stringWithString:textField.text];
if ([string containsString:@" "]) {/**< 过滤空格 */
for (int i = 0; i < string.length; i++) {
unichar c = [string characterAtIndex:i];
NSRange range = NSMakeRange(i, 1);
if (c == ' ') {
[string deleteCharactersInRange:range];
}
}
}
textField.text = string;
}