From 8163beab59d634dd74bf02f5ca12af384339498f Mon Sep 17 00:00:00 2001 From: Yucheng Jin Date: Wed, 5 Mar 2025 09:27:21 -0800 Subject: [PATCH] fix: eliminate scroll jitter in iOS text input when editing in upper region --- ios/PasteTextInput.mm | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/ios/PasteTextInput.mm b/ios/PasteTextInput.mm index dd50053..bac9d1e 100644 --- a/ios/PasteTextInput.mm +++ b/ios/PasteTextInput.mm @@ -597,9 +597,18 @@ - (void)_setAttributedString:(NSAttributedString *)attributedString if ([self _textOf:attributedString equals:_backedTextInputView.attributedText]) { return; } + + // Save current scroll position + CGPoint originalOffset = _backedTextInputView.contentOffset; + + // Temporarily disable scrolling animations + BOOL originalScrollEnabled = _backedTextInputView.scrollEnabled; + _backedTextInputView.scrollEnabled = NO; + UITextRange *selectedRange = _backedTextInputView.selectedTextRange; NSInteger oldTextLength = _backedTextInputView.attributedText.string.length; _backedTextInputView.attributedText = attributedString; + if (selectedRange.empty) { // Maintaining a cursor position relative to the end of the old text. NSInteger offsetStart = [_backedTextInputView offsetFromPosition:_backedTextInputView.beginningOfDocument @@ -612,6 +621,13 @@ - (void)_setAttributedString:(NSAttributedString *)attributedString notifyDelegate:YES]; } [self _restoreTextSelection]; + + // Restore scroll position immediately + [_backedTextInputView setContentOffset:originalOffset animated:NO]; + + // Re-enable scrolling with original state + _backedTextInputView.scrollEnabled = originalScrollEnabled; + _lastStringStateWasUpdatedWith = attributedString; }