Skip to content

Instantly share code, notes, and snippets.

@steipete
Created November 28, 2013 11:15
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save steipete/7690343 to your computer and use it in GitHub Desktop.
Save steipete/7690343 to your computer and use it in GitHub Desktop.
Works around the issue where UITextView doesn't scroll to the new line until there's a character in there. Super horrible workaround.
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
// Horrible HACK for bug in UITextView where it won't scroll to the new line after pressing enter in the text view.
// I feel dirty for having to write this. Buggy at least within iOS 7.0 - 7.1b1.
if (PSPDFIsUIKitFlatMode() && [text isEqualToString:@"\n"] && range.location == textView.text.length) {
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^{
CGPoint contentOffset = textView.contentOffset;
CGFloat fontSize = textView.font.pointSize;
if (textView.contentSize.height - fontSize > textView.bounds.size.height) {
contentOffset.y += fontSize + 2.f; // add spacing
[textView setContentOffset:contentOffset animated:NO];
}
});
}
return YES;
}
@muhammadbassio
Copy link

This was my solution, seems a lot like yours :)

- (void)textViewDidChange:(UITextView *)textView {

    if (textView.text.length > 0) {
        if ([[NSCharacterSet whitespaceAndNewlineCharacterSet] characterIsMember:[textView.text characterAtIndex:([textView.text length] - 1)]]) {
            textView.contentOffset = CGPointMake(0, textView.contentSize.height - textView.frame.size.height + textView.font.pointSize + 2);
        }
    }

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment