UITextView输入时,最下面一行被遮挡

当我们使用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];
}
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容