Use AllInputNo to record the maximum number of input characters. canInputNo to record the remaining input characters.
@property (nonatomic ,assign) NSInteger AllInputNo;
@property (nonatomic ,assign) NSInteger canInputNo;
Then there are mainly two methods:
- (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;
}