iOS8的UITextView输入光标显示不全的hack

在iOS8及以下版本的系统上,在定高的UITextView中,输入内容超过Text View高度后,输入光标有时会在Text View的底部显示不全,如how-to-make-a-uitextview-scroll-while-typing-editing中截图所描述。

尝试了各种方案,挑选了一种体验较好的。在textViewDidChanged:中,检测到正在编辑的区域在文字最下行,无动画滚动到结尾:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
- (void)textViewDidChange:(UITextView *)textView
{
    //hack for iOS8
    if (isLessThanIOS9)//in iOS9 Apple has already fixed this bug
    {
        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)//If at the bottom of text view
        {
            //disable animation. Otherwise, when a input confirm scroll animation is doing, input new text, animation will re-do from animation beginning, which looks strange.
            [UIView setAnimationsEnabled:NO];

            //scroll to text end
            [textView scrollRangeToVisible:NSMakeRange([textView.text length], 0)];
            [UIView setAnimationsEnabled:YES];
        }
    }
}

Over

Comments