iOS 中 UITextView 限制最大输入字数
显示UITextView最大输入字数的代码
1 -(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text 2 { 3 //如果是删除减少字数,都返回允许修改 4 if ([text isEqualToString:@""]) { 5 return YES; 6 } 7 if (range.location>= MaxCount) 8 { 9 return NO; 10 } 11 else 12 { 13 return YES; 14 } 15 } 16 - (void)textViewDidChange:(UITextView *)textView { 17 [self updateRemainCount]; 18 } 19 - (void)updateRemainCount { 20 long count = MAX(0, (int)(MaxCount - self.textView.text.length)); 21 self.lbRemainCount.text = [NSString stringWithFormat:@"当前可输入文字:%ld",count]; 22 }