</pre><p></p><p><strong><span style="font-size:32px;">一、不区分中英文,直接取length</span></strong></p><p>把正在输入的字符也加上。是否超过界限</p><pre code_snippet_id="262117" snippet_file_name="blog_0328_3_1879925" name="code" class="objc"><span style="font-family:Arial, Helvetica, sans-serif;"></span>
#pragma mark -键入Done时,插入换行符,然后执行addBookmark#pragma mark -UITextView delegate- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{//判断加上输入的字符,是否超过界限NSString *str = [NSString stringWithFormat:@"%@%@", textView.text, text];if (str.length > 140){if (textAlert == nil) {textAlert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"字符个数不能大于140" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];textAlert.tag = 19;[textAlert show];}textView.text = [textView.text substringToIndex:140];return NO;}return YES;}
二、区分中英文,一个中文占3个字节
int len = [textField.text lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"==1 text:%@,length:%d",textField.text,len);
一个中文占3个字节,其他是1个
代码可先写在上面这个函数里,但是不好控制,你们试试知道了,我是用另一种方法做的,如下:
-(void)viewWillAppear:(BOOL)animated{[super viewWillAppear:animated];[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textFiledEditChanged:) name:@"UITextViewTextDidChangeNotification" object:contentTextView];}-(void)viewWillDisappear:(BOOL)animated{[super viewWillDisappear:animated];[[NSNotificationCenter defaultCenter] removeObserver:self name:@"UITextViewTextDidChangeNotification" object:contentTextView];}
-(void)textFiledEditChanged:(NSNotification *)obj{UITextView *textField = (UITextView *)obj.object;NSString *alertMessage = [NSString stringWithFormat:@"最大字符数为%d",self.maxWord];NSString *toBeString = textField.text;NSString *lang = [[UITextInputMode currentInputMode] primaryLanguage]; // 键盘输入模式if ([lang isEqualToString:@"zh-Hans"]) { // 简体中文输入,包括简体拼音,健体五笔,简体手写UITextRange *selectedRange = [textField markedTextRange];//获取高亮部分UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];// 没有高亮选择的字,则对已输入的文字进行字数统计和限制if (!position) {int len = [textField.text lengthOfBytesUsingEncoding:NSUTF8StringEncoding];NSLog(@"==1 text:%@,length:%d",textField.text,len);if (len > self.maxWord) {if (!textAlert) {textAlert = [[UIAlertView alloc] initWithTitle:@"提示" message:alertMessage delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];[textAlert show];}else{[textAlert show];}textField.text = lastContentString;}else{isAboveMaxWord = NO;lastContentString = toBeString;}}// 有高亮选择的字符串,则暂不对文字进行统计和限制else{}}// 中文输入法以外的直接对其统计限制即可,不考虑其他语种情况else{int len = [textField.text lengthOfBytesUsingEncoding:NSUTF8StringEncoding];NSLog(@"==2 text:%@,length:%d",textField.text,len);if (len > self.maxWord) {if (!textAlert) {textAlert = [[UIAlertView alloc] initWithTitle:@"提示" message:alertMessage delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];[textAlert show];}else{[textAlert show];}textField.text = lastContentString;}else{lastContentString = toBeString;}}}
注意:lastContentString 是定义的一个NSString,用来记录最后输入的字符串,因为直接通过substringToIndex:去截取字符串会报错,因为我们输入中文计算长度是转码了的!