Open hpique opened 10 years ago
I have no idea why the above happens but this could be a possible workaround:
- (void)scrollToVisibleCaretAnimated:(BOOL)animated {
UITextPosition *selectedTextRangeEnd = self.selectedTextRange.end;
CGRect rect = [self caretRectForPosition:selectedTextRangeEnd];
UITextPosition *givenCaretPosition = [self closestPositionToPoint:CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect))];
if ([self comparePosition:givenCaretPosition toPosition:selectedTextRangeEnd] == NSOrderedSame)
{
[self scrollRectToVisibleConsideringInsets:rect animated:animated];
}
}
:+1: that solved my issue as well
I am seeing this issue as well. When a newline is added (with a character or a line break character) the caret is not always properly scrolled into view. A lot of the time the last line is cut off. The above does fix the issue but now the text jumps around when the frame is equal to the height of the text.
I can fix the text jumping by changing
contentOffset.y = CGRectGetMinY(rect) - insets.top;
to
contentOffset.y = MAX(CGRectGetMinY(rect) - insets.top, 0);
in scrollRectToVisibleConsideringInsets:animated:
@joshuafeldman thanks for sharing :+1:
The textView scrolls to the wrong offset when reaching the end of texts with newlines. The problem appears to be
carectRectForPosition:
, which reports the wrong rect for the end of document. See log and animated gif below.Steps to reproduce:
Log: