Skip to content

Instantly share code, notes, and snippets.

@jacksonh
Last active January 2, 2016 02:49
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jacksonh/8239351 to your computer and use it in GitHub Desktop.
Save jacksonh/8239351 to your computer and use it in GitHub Desktop.
// http://stackoverflow.com/a/19277383
//
- (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];
}];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment