I have this UITextView that is updated in a loop. Text is appended to the end of the text view at every loop. The textview updates but does not scroll to show the end of the text view.
The method I use to append the text is:
- (void)appendText:(NSString*)text toTextView:(UITextView *)textView
{
dispatch_async(dispatch_get_main_queue(), ^{
NSAttributedString* attr = [[NSAttributedString alloc] initWithString:text];
[[textView textStorage] appendAttributedString:attr];
[textView scrollRangeToVisible:NSMakeRange([[textView text] length], 0)];
[textView setNeedsDisplay];
[textView setNeedsLayout];
});
}
The textview updates with text but continues to show just the top.
this is compiled for iOS 7+ only.
Other answers on SO are not solving this.
any clues?
You might want to see this: https://github.com/steipete/PSPDFTextView
Basically, you need to give UITextView some time to do the calculation. Don't scroll immediately, wait for a few moments (~0.2sec) before trying to scroll the text view. And, avoid having blank line at the end of text. You needed to add at least one character in the last line. (Space works for me)