1、定义一个事件:
-(IBAction)limitLength:(UITextField *)sender { bool isChinese;//推断当前输入法是否是中文 if ([[[UITextInputMode currentInputMode] primaryLanguage] isEqualToString: @"en-US"]) { isChinese = false; } else { isChinese = true; } if(sender == self.txtName) { // 8位 NSString *str = [[self.txtName text] stringByReplacingOccurrencesOfString:@"?" withString:@""]; if (isChinese) { //中文输入法下 UITextRange *selectedRange = [self.txtName markedTextRange]; //获取高亮部分 UITextPosition *position = [self.txtName positionFromPosition:selectedRange.start offset:0]; // 没有高亮选择的字,则对已输入的文字进行字数统计和限制 if (!position) { NSLog(@"汉字"); if ( str.length>=9) { NSString *strNew = [NSString stringWithString:str]; [self.txtName setText:[strNew substringToIndex:8]]; } } else { NSLog(@"输入的英文还没有转化为汉字的状态"); } }else{ NSLog(@"str=%@; 本次长度=%d",str,[str length]); if ([str length]>=9) { NSString *strNew = [NSString stringWithString:str]; [self.txtName setText:[strNew substringToIndex:8]]; } } } }
2、对UITextField控件加入监听事件:
//UIControlEventEditingChanged [self.txtName addTarget:self action:@selector(limitLength:) forControlEvents:UIControlEventEditingChanged];
时间: 2024-10-11 16:29:13