不错的判断 UITextView 内容不超过20个字符串的方法

 1 - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
 2     
 3     if ([text isEqualToString:@"
"]){
 4         [self onSendBarrage:nil];
 5         return YES;
 6     }
 7     NSMutableAttributedString *attrM = [[NSMutableAttributedString alloc]initWithAttributedString:textView.attributedText];
 8     if (textView == self.textFieldBarrage)
 9     {
10         if (attrM.length > MAX_INPUT_LEN) {
11             NSRange range = NSMakeRange(0, MAX_INPUT_LEN);
12             textView.attributedText = [attrM attributedSubstringFromRange:range];
13         }
14         [self fixChatButtonStateWithTextViewLength:attrM.length];
15     }
16     return YES;
17 }

以上代码的好处主要在于,先提前计算用户输入的内容,再根据推断出的输入框结果,对输入框的字符进行截取.

原文地址:https://www.cnblogs.com/lz465350/p/5199272.html