Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITextView in iOS7 clips the last line of text string

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!

like image 919
ryan Avatar asked Sep 23 '13 18:09

ryan


2 Answers

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];         }];     } } 
like image 156
davidisdk Avatar answered Sep 25 '22 16:09

davidisdk


The solution I found here was to add a one line fix after you create a UITextView:

self.textview.layoutManager.allowsNonContiguousLayout = NO; 

This one line fixed three issues I had creating a UITextView-based code editor with syntax highlighting on iOS7:

  1. Scrolling to keep text in view when editing (the issue of this post)
  2. UITextView occasionally jumping around after dismissing the keyboard
  3. UITextView random scrolling jumps when trying to scroll the view

Note, I did resize the whole UITextView when the keyboard is shown/hidden.

like image 23
gmyers Avatar answered Sep 23 '22 16:09

gmyers