Skip to content

Commit dcaa33e

Browse files
j-piaseckifacebook-github-bot
authored andcommitted
Add support for numberOfLines for TextInput on iOS (facebook#49549)
Summary: `TextInput` component has been missing support for `numberOfLines` prop on iOS, this PR adds it. ## Changelog: [IOS] [ADDED] - Add support for `numberOfLines` prop on `TextInput` Pull Request resolved: facebook#49549 Test Plan: Tested on RNTester and added a new case utilizing the prop Reviewed By: cipolleschi Differential Revision: D69915133 Pulled By: j-piasecki fbshipit-source-id: b6a86bc64bd3c2129a64e99c9bcec9cf5bfde3bc
1 parent 53ff613 commit dcaa33e

File tree

4 files changed

+19
-2
lines changed

4 files changed

+19
-2
lines changed

packages/react-native/Libraries/Components/TextInput/RCTTextInputViewConfig.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ const RCTTextInputViewConfig = {
142142
placeholder: true,
143143
autoCorrect: true,
144144
multiline: true,
145+
numberOfLines: true,
145146
textContentType: true,
146147
maxLength: true,
147148
autoCapitalize: true,

packages/react-native/Libraries/Components/TextInput/TextInput.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1626,6 +1626,7 @@ function InternalTextInput(props: TextInputProps): React.Node {
16261626
focusable={tabIndex !== undefined ? !tabIndex : focusable}
16271627
mostRecentEventCount={mostRecentEventCount}
16281628
nativeID={id ?? props.nativeID}
1629+
numberOfLines={props.rows ?? props.numberOfLines}
16291630
onBlur={_onBlur}
16301631
onChange={_onChange}
16311632
onContentSizeChange={props.onContentSizeChange}

packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTTextLayoutManager.mm

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,17 @@ - (NSTextStorage *)_textStorageAndLayoutManagerWithAttributesString:(NSAttribute
232232
layoutManager.usesFontLeading = NO;
233233
[layoutManager addTextContainer:textContainer];
234234

235+
// A workaround for the issue with empty line measurement:
236+
// When maximumNumberOfLines is set to N and N+1 line is empty, the returned
237+
// measurement is for N+1 lines. Adding any character to that line results
238+
// in the correct measurement.
239+
if (attributedString.length > 0 && [[attributedString string] characterAtIndex:attributedString.length - 1] == '\n') {
240+
NSMutableAttributedString *mutableString =
241+
[[NSMutableAttributedString alloc] initWithAttributedString:attributedString];
242+
[mutableString replaceCharactersInRange:NSMakeRange(attributedString.length - 1, 1) withString:@"\n "];
243+
attributedString = mutableString;
244+
}
245+
235246
NSTextStorage *textStorage = [[NSTextStorage alloc] initWithAttributedString:attributedString];
236247

237248
RCTApplyBaselineOffset(textStorage);

packages/rn-tester/js/examples/TextInput/TextInputExample.ios.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,6 @@ function KeyboardShortcutsExample() {
304304
const styles = StyleSheet.create({
305305
multiline: {
306306
height: 50,
307-
marginBottom: 4,
308307
},
309308
multilinePlaceholderStyles: {
310309
letterSpacing: 10,
@@ -598,7 +597,7 @@ const textInputExamples: Array<RNTesterModuleExample> = [
598597
title: 'Multiline',
599598
render: function (): React.Node {
600599
return (
601-
<View>
600+
<View style={{gap: 4}}>
602601
<ExampleTextInput
603602
placeholder="multiline text input"
604603
multiline={true}
@@ -620,6 +619,11 @@ const textInputExamples: Array<RNTesterModuleExample> = [
620619
multiline={true}
621620
style={styles.multiline}
622621
/>
622+
<ExampleTextInput
623+
placeholder="multiline text input with max 4 lines"
624+
numberOfLines={4}
625+
multiline={true}
626+
/>
623627
<ExampleTextInput
624628
placeholder="uneditable multiline text input"
625629
editable={false}

0 commit comments

Comments
 (0)