Last active
January 2, 2016 02:49
-
-
Save jacksonh/8239351 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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