UITextView in iOS7 clips the last line of text string

ios7uitextview

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 causes the text to be "clipped". I've tried setting it's clipsToBound property to NO but it still clips the text.

I don't want to call on "setContentOffset:animated" because for one: that's very hacky solution.. secondly: if the cursor was in the middle (vertically) of our textview, it'll cause unwanted scrolling.

Here's a screenshot.

enter image description here

Any help would be greatly appreciated!

Thanks!

Best Answer

The problem is due to iOS 7. In the text view delegate, add this code:

- (void)textViewDidChange:(UITextView *)textView {
    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 ) {
        // We are at the bottom of the visible text and introduced a line feed, scroll down (iOS 7 does not do it)
        // Scroll caret to visible area
        CGPoint offset = textView.contentOffset;
        offset.y += overflow + 7; // leave 7 pixels margin
        // Cannot animate with setContentOffset:animated: or caret will not appear
        [UIView animateWithDuration:.2 animations:^{
            [textView setContentOffset:offset];
        }];
    }
}