首页 > 代码库 > UITextField限制输入字数高亮状态下输入框内真实类容与textFiled(解决中文输入问题)
UITextField限制输入字数高亮状态下输入框内真实类容与textFiled(解决中文输入问题)
不多说,坑爹的玩意,直接上代码:
static const NSInteger kMaxLength = 10;
1.[_textField addTarget:self action:@selector(textFieldChange) forControlEvents:UIControlEventEditingChanged];
2.
//限定主题字数 10个
- (void)textFieldChange{
NSString *toBeString = self.textField.text;
NSString *lang = [[UITextInputMode currentInputMode] primaryLanguage];
if([lang isEqualToString:@"zh-Hans"]){ //简体中文输入,包括简体拼音,健体五笔,简体手写
UITextRange *selectedRange = [self.textField markedTextRange];
UITextPosition *position = [self.textField positionFromPosition:selectedRange.start offset:0];
if (!position){//非高亮
if (toBeString.length > kMaxLength) {
[self.view makeToast:@"您最多可以输入10个字" duration:1 position:@"top"];
self.textField.text = [toBeString substringToIndex:kMaxLength];
}
}
}else{//中文输入法以外
if (toBeString.length > kMaxLength) {
[self.view makeToast:@"您最多可以输入10个字" duration:1 position:@"top"];
self.textField.text = [toBeString substringToIndex:kMaxLength];
}
}
}
UITextField限制输入字数高亮状态下输入框内真实类容与textFiled(解决中文输入问题)