当我们使用UITextView时,出现出输入文字时, 输入文字的高度超出UITextView的高度一行时才开始滚动,而且最下面一行是被遮挡的.
当出现这种情况的时候我们可以使用一下方法来解决:
1.设置内边距
<pre>contentTextView.contentInset = UIEdgeInsetsMake(0.0, 0.0, 15.0, 0.0);
2.在遵守代理之后在代理方法textViewDidChange中:
<pre>-(void)textViewDidChange:(UITextView *)textView {
NSRange selection = textView.selectedRange;
if (selection.location + selection.length == [textView.text length]) {
CGRect caretRect = [textView caretRectForPosition:textView.selectedTextRange.start];
CGFloat overflow = caretRect.origin.y + caretRect.size.height - (textView.contentOffset.y + textView.bounds.size.height - textView.contentInset.bottom - textView.contentInset.top);
if (overflow > 0.0f) {
CGPoint offset = textView.contentOffset;
offset.y += overflow + 7.0f;
[UIView animateWithDuration:0.2f animations:^{
[textView setContentOffset:offset];
}];
}
} else {
[textView scrollRangeToVisible:selection];
}
}