當(dāng)我們使用UITextView時(shí),出現(xiàn)出輸入文字時(shí), 輸入文字的高度超出UITextView的高度一行時(shí)才開(kāi)始滾動(dòng),而且最下面一行是被遮擋的.
當(dāng)出現(xiàn)這種情況的時(shí)候我們可以使用一下方法來(lái)解決:
1.設(shè)置內(nèi)邊距
<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];
}
}