举个例子:限制输入字符为200
-(void)textViewDidChange:(UITextView*)textView{
UITextRange *selectedRange = [textView markedTextRange];
//获取高亮部分
UITextPosition *pos = [textView positionFromPosition:selectedRange.start offset:0];
//如果在变化中是高亮部分在变,就不要计算字符了
if(selectedRange && pos) {return;}
self.textViewLimitLabel.text = [NSString stringWithFormat:@"%lu/200", (unsigned long)textView.text.length];
if( (unsigned long)textView.text.length>200) {
// 对超出的部分进行剪切
textView.text= [textView.text substringToIndex:200];
self.textViewLimitLabel.text = @"200/200";
}
}