Skip to content

Commit e210c7c

Browse files
fabOnReactTitozzz
authored andcommitted
Fix Multiline TextInput with a fixed height scrolls to the bottom when prepending new lines to the text (#38679)
Summary: ### Please re-state the problem that we are trying to solve in this issue. Multiline TextInput with a fixed height will scroll to the bottom of the screen when prepending new lines to the text. ### What is the root cause of that problem? The issue is caused by iOS UITextView: - The cursor moves to the end of the text when prepending new lines. - Moving the cursor to the end of the text triggers the scroll to the bottom. The behavior was reproduced on an iOS App (without react-native). The example included below implements a Component RCTUITextView based on UITextView, which modifies the UITextView attributedText with the textViewDidChange callback (source code available in this [comment](Expensify/App#19507 (comment))). Adding a new line on top of the UITextView on iOS results in: Issue 1) The cursor moves to the end of TextInput text Issue 2) The TextInput scrolls to the bottom <details><summary>Reproducing the issue on an iOS App without react-native</summary> <p> <video src="https://user-images.githubusercontent.com/24992535/246601549-99f480f3-ce80-4678-9378-f71c8aa67e17.mp4" width="900" /> </p> </details> Issue 1) is already fixed in react-native, which restores the previous cursor position (on Fabric with [_setAttributedString](https://github.com/fabriziobertoglio1987/react-native/blob/71e7bbbc2cf21abacf7009e300f5bba737e20d17/packages/react-native/React/Fabric/Mounting/ComponentViews/TextInput/RCTTextInputComponentView.mm#L600-L610)) after changing the text. Issue 2) needs to be fixed in react-native. ### What changes do you think we should make in order to solve the problem? Setting the correct TextInput scroll position after re-setting the cursor. ## Changelog: [IOS] [FIXED] - Fix Multiline TextInput with a fixed height scrolls to the bottom when changing AttributedText Pull Request resolved: #38679 Test Plan: Fabric (reproduces on controlled/not controlled TextInput example): | Before | After | | ----------- | ----------- | | <video src="https://github.com/facebook/react-native/assets/24992535/e06b31fe-407d-4897-b608-73e0cc0f224a" width="350" /> | <video src="https://github.com/facebook/react-native/assets/24992535/fa2eaa31-c616-43c5-9596-f84e7b70d80a" width="350" /> | Paper (reproduces only on controlled TextInput example): ```javascript function TextInputExample() { const [text, setText] = React.useState(''); return ( <View style={{marginTop: 200}}> <TextInput style={{height: 50, backgroundColor: 'white'}} multiline={true} value={text} onChangeText={text => { setText(text); }} /> </View> ); } ``` | Before | After | | ----------- | ----------- | | <video src="https://github.com/facebook/react-native/assets/24992535/6cb1f2de-717e-4dce-be0a-644f6a051c08" width="350" /> | <video src="https://github.com/facebook/react-native/assets/24992535/dee6edb6-76c6-48b0-b78f-99626235d30e" width="350" /> | Reviewed By: sammy-SC, cipolleschi Differential Revision: D48674090 Pulled By: NickGerleman fbshipit-source-id: 349e7b0910e314ec94b45b68c38571fed41ef117
1 parent d9e4278 commit e210c7c

File tree

4 files changed

+16
-0
lines changed

4 files changed

+16
-0
lines changed

packages/react-native/Libraries/Text/TextInput/Multiline/RCTUITextView.mm

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,12 @@ - (void)setSelectedTextRange:(UITextRange *)selectedTextRange notifyDelegate:(BO
163163
[super setSelectedTextRange:selectedTextRange];
164164
}
165165

166+
// After restoring the previous cursor position, we manually trigger the scroll to the new cursor position (PR 38679).
167+
- (void)scrollRangeToVisible:(NSRange)range
168+
{
169+
[super scrollRangeToVisible:range];
170+
}
171+
166172
- (void)paste:(id)sender
167173
{
168174
_textWasPasted = YES;

packages/react-native/Libraries/Text/TextInput/RCTBackedTextInputViewProtocol.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ NS_ASSUME_NONNULL_BEGIN
4343
// If the change was a result of user actions (like typing or touches), we MUST notify the delegate.
4444
- (void)setSelectedTextRange:(nullable UITextRange *)selectedTextRange NS_UNAVAILABLE;
4545
- (void)setSelectedTextRange:(nullable UITextRange *)selectedTextRange notifyDelegate:(BOOL)notifyDelegate;
46+
- (void)scrollRangeToVisible:(NSRange)selectedTextRange;
4647

4748
// This protocol disallows direct access to `text` property because
4849
// unwise usage of it can break the `attributeText` behavior.

packages/react-native/Libraries/Text/TextInput/Singleline/RCTUITextField.mm

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,11 @@ - (void)setSelectedTextRange:(UITextRange *)selectedTextRange notifyDelegate:(BO
201201
[super setSelectedTextRange:selectedTextRange];
202202
}
203203

204+
- (void)scrollRangeToVisible:(NSRange)range
205+
{
206+
// Singleline TextInput does not require scrolling after calling setSelectedTextRange (PR 38679).
207+
}
208+
204209
- (void)paste:(id)sender
205210
{
206211
_textWasPasted = YES;

packages/react-native/React/Fabric/Mounting/ComponentViews/TextInput/RCTTextInputComponentView.mm

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,9 @@ - (void)_setAttributedString:(NSAttributedString *)attributedString
594594
UITextRange *selectedRange = _backedTextInputView.selectedTextRange;
595595
NSInteger oldTextLength = _backedTextInputView.attributedText.string.length;
596596
_backedTextInputView.attributedText = attributedString;
597+
// Updating the UITextView attributedText, for example changing the lineHeight, the color or adding
598+
// a new paragraph with \n, causes the cursor to move to the end of the Text and scroll.
599+
// This is fixed by restoring the cursor position and scrolling to that position (iOS issue 652653).
597600
if (selectedRange.empty) {
598601
// Maintaining a cursor position relative to the end of the old text.
599602
NSInteger offsetStart = [_backedTextInputView offsetFromPosition:_backedTextInputView.beginningOfDocument
@@ -604,6 +607,7 @@ - (void)_setAttributedString:(NSAttributedString *)attributedString
604607
offset:newOffset];
605608
[_backedTextInputView setSelectedTextRange:[_backedTextInputView textRangeFromPosition:position toPosition:position]
606609
notifyDelegate:YES];
610+
[_backedTextInputView scrollRangeToVisible:NSMakeRange(offsetStart, 0)];
607611
}
608612
[self _restoreTextSelection];
609613
_lastStringStateWasUpdatedWith = attributedString;

0 commit comments

Comments
 (0)