UITextView text length limitation

iOS8 以上键盘的自动联想功能使UITextView的长度限制产生一些bug

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#define textLimitLength 50
#pragma mark - UITextViewDelegate
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if (textView.text.length >= textLimitLength && text.length > range.length) {
return NO;
}
return YES;
}
- (void)textViewDidChange:(UITextView *)textView {
if (textView.markedTextRange == nil && textView.text.length > textLimitLength) {
textView.text = [textView.text substringToIndex:textLimitLength];
}
}