I have a method for text changed in my web view to detect the visible rect for the current caret position.
UITextPosition *endPos = self.selectedTextRange.end;
CGRect rect = [self caretRectForPosition:endPos];
[self scrollRectToVisible:rect animated:NO];
It works great except when I'm at the end of the document and I press the the delete key on the keyboard. In this case, it scrolls to the beginning of the document, which is unexpected.
I've had a similar problem... it seems to be a timing problem in the text view. My solution is:
A: Detect the invalid result from caretRectForPosition. In my case, the invalid coordinates seem always to be either large negative values (-1.0 seems to be i.O.!) or 'infinite' for origin.y.
B: Re-ask the text view for the caret position after a short period of time. I checked a few values for delay; 0.05 seems to be largely enough.
The code: