A sustainable solution should be delayed in the following situations:
(1.) a text representation displaying an attribute string
(2.) a new line created by pressing the return key on the keyboard
(3.) a new line created by entering text that overflows onto the next line
(4.) copy and paste text
(5.) a new line created by pressing the return key for the first time (see 3 steps in OP)
(6.) device rotation
(7.) In some case, I cannot think that you ...
To meet these requirements in iOS 7.1, it seems that you still need to manually scroll to the carriage.
Typically, to view solutions that manually scroll to the caret when the text view delegate method textViewDidChange: is called . However, I found that this method does not satisfy situation No. 5 above. Even calling layoutIfNeeded before scrolling to the caret did not help. Instead, I had to scroll to the carriage inside the CATransaction termination CATransaction :
// this seems to satisfy all of the requirements listed above–if you are targeting iOS 7.1 - (void)textViewDidChange:(UITextView *)textView { if ([textView.text hasSuffix:@"\n"]) { [CATransaction setCompletionBlock:^{ [self scrollToCaretInTextView:textView animated:NO]; }]; } else { [self scrollToCaretInTextView:textView animated:NO]; } }
Why does this work? I have no idea. You will have to ask an Apple engineer.
For completeness, here is all the code related to my solution:
If you find a situation where this does not work, let me know.
bilobatum Mar 13 '14 at 5:00 2014-03-13 05:00
source share