UITextView in iOS7 clips the last line of text string

后端 未结 11 1307
轻奢々
轻奢々 2020-12-04 09:30

UITextView in iOS7 has been really weird. As you type and are entering the last line of your UITextView, the scroll view doesn\'t scroll to the bottom like it should and it

11条回答
  •  囚心锁ツ
    2020-12-04 09:39

    Heres a modified version of the selected answer by davidisdk.

    - (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];
        }
    }
    

    I was getting a bug that when the textView's content size is larger then the bounds and the cursor is offscreen (such as using a keyboard and pressing the arrow key) the textView wouldn't animate to the text being inserted.

提交回复
热议问题