使用AllInputNo来记录限制最大输入字数.canInputNo来记录剩余可输入字数.
@property (nonatomic ,assign) NSInteger AllInputNo;
@property (nonatomic ,assign) NSInteger canInputNo;
然后是主要是两个方法:
- (BOOL)textView:(YYTextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
//输入之后所有的字符是否超出了界限,能够检测出粘贴
NSString *str = [NSString stringWithFormat:@"%@%@", textView.text, text];
if (str.length > self.AllInputNo) {
textView.text = [str substringToIndex:self.AllInputNo];
// return NO;
}
return YES;
}
- (void)textViewDidChange:(YYTextView *)textView {
//该判断用于联想输入
if (textView.text.length > self.AllInputNo) {
textView.text = [textView.text substringToIndex:self.AllInputNo];
}
self.canInputNo = self.AllInputNo - textView.text.length;
}