Skip to content
This repository was archived by the owner on Nov 17, 2025. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions PSPDFTextView/PSPDFTextView.m
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ - (void)scrollRectToVisibleConsideringInsets:(CGRect)rect animated:(BOOL)animate
// Calculate new content offset.
CGPoint contentOffset = self.contentOffset;
if (CGRectGetMinY(rect) < CGRectGetMinY(visibleRect)) { // scroll up
contentOffset.y = CGRectGetMinY(rect) - insets.top;
contentOffset.y = MAX(CGRectGetMinY(rect) - insets.top, 0);
}else { // scroll down
contentOffset.y = CGRectGetMaxY(rect) + insets.bottom - CGRectGetHeight(self.bounds);
}
Expand Down Expand Up @@ -144,11 +144,12 @@ - (void)scheduleScrollToVisibleCaretWithDelay:(NSTimeInterval)delay {
}

- (void)scrollToVisibleCaretAnimated:(BOOL)animated {
const CGRect caretRect = [self caretRectForPosition:self.selectedTextRange.end];
// The caret is sometimes off by a pixel. When this happens, scrolling to its rect produces a little bounce.
// To avoid this, we scroll to its center instead.
const CGRect caretCenterRect = CGRectMake(CGRectGetMidX(caretRect), CGRectGetMidY(caretRect), 0, 0);
[self scrollRectToVisibleConsideringInsets:caretCenterRect animated: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];
}
}

- (void)scrollToVisibleCaret {
Expand Down