- (void) textViewDidChange:(UITextView *)textView
{
if (textView.text.length > 5000) { // 限制5000字内
textView.text = [textView.text substringToIndex:5000];
}
iOS7之后出现,通过改变scrolloview的setContentOffset处理位置
CGRect line = [_textView caretRectForPosition:
_textView.selectedTextRange.start];
CGFloat overflow = line.origin.y + line.size.height - (_textView.contentOffset.y + _textView.bounds.size.height - _textView.contentInset.bottom - _textView.contentInset.top);
if ( overflow > 0 ) {
// We are at the bottom of the visible text and introduced a line feed, scroll down (iOS 7 does not do it)
// Scroll caret to visible area
CGPoint offset = _textView.contentOffset;
offset.y += 5; // leave 5 pixels margin
// Cannot animate with setContentOffset:animated: or caret will not appear
[UIView animateWithDuration:.2 animations:^{
[_textView setContentOffset:offset];
}];
}
[self setTextViewSizeToFit:NO];
}