UITextView限制字数与行数
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{ CGFloat textHeight = textView.contentSize.height; NSLog(@"%@",[NSString stringWithFormat:@"%f",textHeight]); if (range.location > COMMENTMAXTEXT )//([textView.text length] + [text length] - range.length >250 { _textView.text = [_textView.text substringToIndex:COMMENTMAXTEXT]; return NO; } else if (textView.contentSize.height >= COMMENTMAXROWHEIGHT ){//正好是最8行 textView.text = [textView.text substringToIndex:[textView.text length]-1]; return NO; } else { return YES; } } - (void)textViewDidChange:(UITextView *)textView{ if(textView.contentSize.height >= COMMENTMAXROWHEIGHT) { [MBProgressHUD showError:@"最多只能输入8行"]; _textView.text = [textView.text substringToIndex:[textView.text length]-1]; }else if(textView.text.length > COMMENTMAXTEXT){ _textView.text = [_textView.text substringToIndex:COMMENTMAXTEXT]; [MBProgressHUD showError:@"最多只能输入250个字"]; } // NSLog(@"%s",__func__); }
将来的自己,会感谢现在不放弃的自己!