1 - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{ 2 3 if ([text isEqualToString:@"\n"]){ 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 }
以上代码的好处主要在于,先提前计算用户输入的内容,再根据推断出的输入框结果,对输入框的字符进行截取.
时间: 2024-10-31 17:39:54