Skip to content

Center a single-line iOS TextInput's text and keep it on one line - #60378

Open
ide wants to merge 1 commit into
react:mainfrom
expo:@ide/ios-single-line-input-line-height
Open

ide wants to merge 1 commit into
react:mainfrom
expo:@ide/ios-single-line-input-line-height

Conversation

@ide

@ide ide commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Summary:

Fixes #29068.

A single-line TextInput whose style gives its text a paragraph style draws the text wrong in two ways. Both come from the paragraph style that RCTNSTextAttributesFromTextAttributes builds, which replaces the one in UITextField's default text attributes.

Not centered. With a lineHeight larger than its font, the input draws its value and placeholder at the bottom of the line instead of in the middle; with a large enough lineHeight the text crosses the field's bottom border. lineHeight reaches the field as the paragraph style's minimum and maximum line height, and TextKit puts all of the extra leading above the glyphs: a 16 pt system font in a 24 pt line gets about 5 pt of extra leading, all of it above. CSS splits the leading equally above and below (CSS 2, "Leading and half-leading").

Wrapped. With textAlign, writingDirection, lineBreakStrategyIOS, lineHeight or lineBreakModeIOS="wordWrapping", on the input or on a nested <Text>, the input wraps a value wider than the field when it is not being edited (#29068). The first line ends at a word with no ellipsis, and when the field is tall enough for two lines of its font, such as a 16 pt font in a 44 pt field, the second line is drawn too, across the field's bottom border. The placeholder loses its ellipsis the same way. An input with none of those styles ends the line with an ellipsis. The paragraph style is built from a new NSMutableParagraphStyle, whose line break mode is NSLineBreakByWordWrapping (NSParagraphStyle.default), where UITextField's own is NSLineBreakByTruncatingTail, and a text field that is not being edited lays out its attributed text as the paragraph style says.

main on the left and this change on the right, for a value wider than the field with textAlign: 'right' and with lineHeight: 40:

A wide value with textAlign right and with lineHeight 40, on main and with this change

A single-line field now draws with paragraph styles that have neither:

  • NSLineBreakByWordWrapping and NSLineBreakByCharWrapping become NSLineBreakByTruncatingTail.
  • When the input has a lineHeight, the line height is removed. Layout already makes the field as tall as its line, and UITextField centers the font's own line in its text rect.

Both changes are made in the field's default text attributes, which also style the placeholder and typed text, and in the attributed text the field is given. Multi-line inputs are unchanged. The two changes depend on each other: without the line height, two lines of the font fit in a field that held one 40 pt line, so centering alone would draw a second line where main only loses the ellipsis.

lineBreakModeIOS (#46128) was added for the same wrap, reported in #44107, and lineBreakModeIOS="tail" beside textAlign avoids it. A single-line input no longer needs it. The prop still chooses clipping or an ellipsis at the head, middle or tail, which keep to one line and are left alone. Its two wrapping values, "wordWrapping" and "char", now end the line with an ellipsis in a single-line input. They never applied to the text being edited, which scrolls on one line.

A lineHeight smaller than the font's line is drawn the same way. The field is then shorter than its text, which is centered on the field and extends equally above and below it. Chrome and Safari center the text of an <input> whose height is below its line the same way, and clip it to the box. The field clips the placeholder and the text being edited, but not a value that is not being edited, which can overlap the views next to the field. On main the compressed line puts the text above the field, and the text being edited loses its upper part. Keeping the compressed line for such a lineHeight would keep that drawing, so it is not an exception.

The state's attributed string keeps its line heights, because layout measures the field from it. A private attribute records the paragraph style replaced in each range where it is not the input's own, so text written back to the state gets each nested <Text> run's style again. Text with no record, which is the input's own text and text typed into the field, gets the input's paragraph style; it therefore follows the input when its lineHeight changes. The record holds the whole style because a nested <Text> can differ from the input in more than its line height, such as its alignment. The line break mode is not restored: the text written to the state has the truncating styles, and layout measures a single-line input at unlimited width (BaseTextInputShadowNode::getTextConstraints), where wrapping and truncating give the same size. _setAttributedString replaces the line break modes of a state's text and restores the line heights of the field's text before it compares the two, so a state that repeats what the field shows does not set the field's text again.

updateProps: and a change of the content size category set the default text attributes through one method, so a change of the content size category also keeps the event emitter attribute, which main drops there.

Limitations:

  • A single-line input whose lineHeight is smaller than its font's line stays as tall as the lineHeight, as on main. Chrome and Safari ignore such a line-height when they size an <input> with no height, and make it as tall as the font's line.
  • A single-line input without lineHeight whose nested <Text> sets one still draws that text at the bottom of its line.
  • A nested <Text> whose paragraph style equals the input's has no record, so it follows the input when the input's lineHeight changes. The next state from React corrects that by setting the field's text once, as on main.
  • When the input's text is a nested <Text> with its own lineHeight, the first character typed into the empty field, and the first character typed after a tap moves the cursor, carry no record and get the input's line height. A controlled input's next state corrects that by setting the field's text once, as on main.
  • A nested <Text style={{lineHeight: 0}}> in an input with a lineHeight has no record, so the text for the state gives it the input's line height and each state sets the field's text again.
  • A change of lineBreakModeIOS alone does not reach the field, as on main: TextAttributes::operator== leaves lineBreakMode out, so updateProps: sees no change.

Changelog:

[IOS] [FIXED] - Center the text and placeholder of a single-line TextInput vertically within its lineHeight
[IOS] [FIXED] - Keep a single-line TextInput with textAlign or another paragraph style on one line, ending with an ellipsis, when its value is wider than the field; lineBreakModeIOS "wordWrapping" and "char" no longer wrap it

Test Plan:

Xcode 27.1 and an iOS 27.0 simulator (iPhone 18 Pro). No device was used.

Unit tests

xcodebuild test -workspace packages/rn-tester/RNTesterPods.xcworkspace -scheme React-RCTFabric-Unit-Tests \
  -destination 'platform=iOS Simulator,id=<iPhone 18 Pro>' \
  -only-testing:React-RCTFabric-Unit-Tests/RCTTextInputComponentViewTests \
  -only-testing:React-RCTFabric-Unit-Tests/RCTTextInputUtilsTest

All pass. The tests in React/Tests/Text:

  • testSingleLineInputWithParagraphStyleDrawsWideTextOnOneLine renders a 100 pt by 60 pt input with a 16 pt font and a value several times as wide, and counts the lines of text drawn. A single-line input draws one with textAlign: 'right', with a paragraph style that comes only from a nested run, and with lineHeight: 40. A multi-line input draws three. On main the first two draw two lines; with the line height omitted and the state's styles not replaced, the third does.
  • testOnlySingleLineInputReplacesWrappingLineBreakMode reads the line break mode of the field's default text attributes: truncating for a single-line input, also in its placeholder and after a change of the content size category; wrapping after multiline becomes true, and truncating again after it becomes false. lineBreakModeIOS="head" stays NSLineBreakByTruncatingHead. On main the single-line cases are wrapping.
  • testSingleLineInputDrawsWithoutLineHeight, testMultilineInputDrawsWithLineHeight and testInputThatBecomesMultilineDrawsWithLineHeight check the line height of the default text attributes. On main the first fails (40, not 0).
  • testSingleLineInputCentersTextOnTheFieldAtAnyLineHeight renders a field with a 16 pt font and measures how far the center of the drawn text is from the center of the field. It is the same distance, within 1 pt, with no lineHeight, with lineHeight: 40 and with lineHeight: 10. On main the text is 20.5 pt lower at 40 and 9.5 pt higher at 10.
  • testInputWithTextThatBecomesMultilineKeepsNoOmittedLineHeight and testInputWithTextThatBecomesSingleLineDrawsWithoutLineHeight change multiline on a view that holds the input's own text and a nested run of 40. The new view gets the plain string (RCTCopyBackedTextInput), so no record crosses the switch. The multi-line view's text for the state is its text as it is, and a state then gives it the line height again. The single-line field draws all of the text without line height and writes the input's 24 for it; after a state it records 40 for the nested run only. Neither step counts as a change of the text (the event count stays 0).
  • testStateWrittenAfterTypingHasTheLineHeightAndLeavesTheFieldAlone types into an empty focused field with lineHeight: 40 and reads the text _updateState writes to the state: it has the input's line height, and the field's own text has none. It then gives the view a state holding that text, as an uncontrolled input receives, and counts the times the field's attributed text is set (none).
  • testStateEchoingTypedTextLeavesTheFieldAlone and testStateEchoingTextTypedIntoNestedRunLeavesTheFieldAlone type into a focused field with a lineHeight, then give the view the state a controlled input sends back: the same text built from the React tree, with its line heights, its wrapping line break mode and the event count of the change. The nested-run test's text also has a run with the input's line height and its own alignment. They count the sets (none), check the text and the cursor, and then send a state with different text, which sets it once. The nested-run test places the cursor in code, which keeps the run's attributes for the typed text; a tap does not (see the limitations). testStateRepeatingTypedTextLeavesTheFieldAlone does the same for an input with textAlign: 'right' and no lineHeight, with a controlled and an uncontrolled state.
  • testChangingLineHeightChangesItForTheInputsOwnTextOnly changes an input's lineHeight from 24 to 30 while it holds its own text and a nested run of 40: the text for the state then has 30 and 40.
  • RCTTextInputUtilsTest checks the functions. The two wrapping modes become truncating and keep the rest of the style, the four other modes return the same style, and in an attributed string each run's style is replaced while text with no paragraph style gets none. Omitting and restoring per-run paragraph styles gives back the original string, text with no record or no paragraph style gets the input's style, and a restore with a different line height for the input changes only the input's own run, not a run that has the input's line height and differs from it in another part of its style.

Each of these changes to the code fails the tests named, and only those:

Change Failing tests
main's RCTTextInputComponentView.mm ChangingLineHeight, WithTextBecomesMultiline, WithTextBecomesSingleLine, OnlySingleLineInputReplaces, Centers, SingleLineDrawsWithoutLineHeight, DrawsWideTextOnOneLine, EchoingNestedRun, EchoingTypedText, StateWrittenAfterTyping
No line break mode is replaced OnlySingleLineInputReplaces, DrawsWideTextOnOneLine
The default text attributes keep their line break mode ChangingLineHeight, WithTextBecomesSingleLine, OnlySingleLineInputReplaces, EchoingTypedText, StateWrittenAfterTyping
A state's text keeps its line break modes ChangingLineHeight, WithTextBecomesSingleLine, DrawsWideTextOnOneLine, EchoingTypedText
A state's line break modes are replaced only for an input with a lineHeight DrawsWideTextOnOneLine
The comparison runs before a state's line break modes are replaced EchoingNestedRun, EchoingTypedText, RepeatingTypedText
The input's paragraph style is recorded before its line break mode is replaced ChangingLineHeight, WithTextBecomesSingleLine, EchoingTypedText, StateWrittenAfterTyping
A multi-line input replaces line break modes too OnlySingleLineInputReplaces, DrawsWideTextOnOneLine
A content size category change sets main's attributes OnlySingleLineInputReplaces
Every line break mode becomes truncating tail ReplacesEachRunsStyle, OnlySingleLineInputReplaces, ReplacesOnlyModesThatWrap
NSLineBreakByCharWrapping is kept ReplacesOnlyModesThatWrap
Only the first run of an attributed string is replaced ReplacesEachRunsStyle
multiline alone does not recompute the attributes BecomesMultiline, WithTextBecomesMultiline, WithTextBecomesSingleLine, OnlySingleLineInputReplaces
The default text attributes keep the line height ChangingLineHeight, WithTextBecomesSingleLine, SingleLineDrawsWithoutLineHeight
The comparison uses the field's text as drawn EchoingNestedRun, EchoingTypedText, StateWrittenAfterTyping
The state gets the field's text as drawn ChangingLineHeight, WithTextBecomesSingleLine, EchoingNestedRun, EchoingTypedText, StateWrittenAfterTyping
A state's text is set with its line heights ChangingLineHeight, WithTextBecomesMultiline, WithTextBecomesSingleLine, Centers, EchoingNestedRun
The restore ignores records ChangingLineHeight, WithTextBecomesSingleLine, GivesEachRunItsOwn, EchoingNestedRun
The restore ignores the input's style ChangingLineHeight, WithTextBecomesSingleLine, RecordsNothing, GivesEachRunItsOwn, UnrecordedText, EchoingNestedRun, EchoingTypedText, StateWrittenAfterTyping
The input's own style is recorded too ChangingLineHeight, WithTextBecomesSingleLine, GivesEachRunItsOwn
A range is recorded only for a different line height GivesEachRunItsOwn, EchoingNestedRun
A line height below the font's line stays in the drawn text Centers
A multi-line view omits the line height too BecomesMultiline, WithTextBecomesMultiline, MultilineDrawsWithLineHeight
The switch of multiline copies the text with its attributes WithTextBecomesMultiline

The names are shortened: EchoingTypedText is testStateEchoingTypedTextLeavesTheFieldAlone, and so on.

Font scaling is off in these tests: without an application RCTFontSizeMultiplier() is 0. For the same reason the tests report a typed change to the view themselves: UIKit cannot deliver the field's editing-changed action.

RNTester does not install the React-RCTFabric Tests spec. To run it locally (not in this change): add :testspecs => ['Tests'] in scripts/cocoapods/fabric.rb, narrow the spec's source_files to these files, and add an empty Swift file for RCTSwiftUI's Swift runtime.

RNTester on the simulator

RNTester (Debug) built from main and from this branch, with local Playground pages, driven by an XCUITest that types on the software keyboard and reads a readout of each field's onChange text, onSelectionChange range and onLayout height.

lineHeight below the font (font size 20 with lineHeight: 14, and 60 with 42, text "Ágjpqy"):

main This branch
Value, not focused Drawn above the field by the difference between the font's line and the lineHeight (about 30 pt at 60/42), not clipped Centered on the field, extends about 15 pt above and below at 60/42, not clipped
Placeholder Clipped to the field, which shows only the lower part of the glyphs Centered, clipped equally above and below
Value, focused Clipped to the field, which shows only the lower part of the glyphs Centered, clipped equally above and below: the accent and the descenders' ends are cut
Field height after typing into the empty field (20/14) Grows from 16.33 to 26 Stays 16.33

main on the left, this branch on the right; not focused, then with the 60/42 value focused:

A lineHeight below the font's line, not focused

A lineHeight below the font's line, with the 60/42 value focused

Chrome 159.0.8154.6 (headless) and Safari on the iOS 27.0 simulator, for <input style="font-size: 60px; line-height: 42px">: with no height the content box is as tall as the font's line (71 px and 73 px) and nothing is clipped; with height: 42px the text is centered and clipped equally above and below, as on this branch.

The same styles on an input element in Chrome

The same styles on an input element in Safari

Typing. In four single-line fields the test types "hello world", taps inside the text to move the cursor, and types "XY"; in the field with fixed child text it only taps and types "XY". That is 54 keystrokes. The fields: lineHeight: 40 uncontrolled; the same controlled by value; lineHeight: 24 with a child <Text style={{lineHeight: 40}}> that holds the value from state; the same with fixed child text; and font size 20 with lineHeight: 14.

main This branch
Text and cursor after "XY" "XY" at the tapped position with the cursor after "Y" in every field: "hello XYworld" in four, "helloXY world" in the 20/14 field The same
Field height after typing, lineHeight: 40, uncontrolled Falls from 42.33 to 21.33: UIKit gives typed text a paragraph style without the line height Stays 42.33
Field height after typing, other lineHeight: 40 fields 42.33 42.33
Times the field's text was set again in the 54 keystrokes (builds with a log line added in _setAttributedString) 3: the first keystroke in the controlled field and in the nested field, and the "X" in the nested field 2: the first keystroke and the "X" in the nested field

Typing into the uncontrolled lineHeight: 40 field, main on the left and this branch on the right:

Typing into a single-line field with lineHeight 40

With the restore removed from the comparison in _setAttributedString (a build of the lineHeight change alone), the field's text is set 80 times in the 54 keystrokes. The readout still shows the right text and cursor, because _setAttributedString puts the cursor back, so only the count of sets shows the resets.

A value wider than the field, lineHeight: 40 and font size 16, not focused (main on the left, this branch on the right). On main the value is one line, cut at a word, that crosses the field's bottom border. On this branch it is one centered line that ends with an ellipsis, as the fourth input, which has no lineHeight, draws it on both. While the value is edited it scrolls and is clipped at the field's edge with no ellipsis, as on main.

A value wider than a single-line field with lineHeight 40

Values wider than the field with the other paragraph style props: single-line inputs 280 pt wide and 44 pt tall with a 16 pt font and the value "one two three … twelve", not being edited. main is on the left in each image.

Single-line inputs with each paragraph style prop, not being edited

Input main This branch
No paragraph style; letterSpacing: 1; nested <Text> with no style One line with an ellipsis The same
textAlign left, right or center; writingDirection: 'ltr'; lineBreakStrategyIOS="standard"; lineBreakModeIOS="wordWrapping"; nested <Text> with textAlign: 'right' Two lines, the second across the bottom border One line with an ellipsis, aligned as textAlign says
textAlign: 'right' with lineBreakModeIOS="tail" One line with an ellipsis The same
textAlign: 'right', no height (one line tall) One line that ends at a word, no ellipsis One line with an ellipsis
textAlign: 'right', placeholder wider than the field One line that ends at a word, no ellipsis One line with an ellipsis
lineHeight: 40 One line across the field's bottom border that ends at a word, no ellipsis One centered line with an ellipsis
textAlign: 'right', multiline Two lines The same

Natural height, placeholder, multi-line, controlled, lineHeight and nested inputs

After typing. The test types "one two … ten" into an empty input with textAlign: 'right', taps inside the text and types "XY", then focuses another input; it also types "Z" into a controlled input and leaves it. On main both values wrap once the field is left; on this branch each is one line with an ellipsis. The input being edited (the second one here) shows one line that scrolls, with no ellipsis, on both.

Typed and controlled values after the field is left

Typing with textAlign. In each of four fields the test types "hello world", taps inside the text and types "XY" (52 keystrokes): textAlign: 'right' uncontrolled, the same controlled by value, a child <Text style={{textAlign: 'right'}}> holding the value from state, and textAlign: 'center' sized by its text.

main This branch
Text and cursor after "XY" "XY" at the tapped position with the cursor after "Y" in every field The same
Size of the field sized by its text 80.67 by 21.33 after "hello world", 101 by 21.33 after "XY" The same
Times the field's text was set again in the 52 keystrokes 2, both in the nested field: its first keystroke and its "X" The same 2

The four fields after the typing, with the readout above them:

The four textAlign fields after typing, on main and on this branch

Switching multiline. A page toggles multiline on the four inputs above (controlled, nested child, uncontrolled, and uncontrolled with no lineHeight), types a character before each switch, and reads the value and onChange count of the first three. <TextInput> renders a different native component for each mode, so React mounts a new view on every switch, on main and on this branch: the character typed into the uncontrolled lineHeight: 40 input is gone after the switch and the controlled values stay. A log line in _setMultiline: shows it running 9 times, each time on a view with no text. No onChange fires at a switch. The heights of the lineHeight: 40 inputs are 42.33 single-line and 82.33 multi-line (two lines of 40) on both, and the multi-line lines are spaced by the lineHeight.

With lineHeight 24 and 40 on single-line, nested-run and multi-line inputs that are not being edited: on main single-line text sits at the bottom and crosses the border at 40; on this branch it is centered, frames are unchanged, and multi-line draws as before.

Not checked: dictation and marked text (Japanese, Chinese and Korean input), where the comparison uses the plain strings as on main, and right-to-left layout.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Nov 28, 2026
@github-actions github-actions Bot added the Meta Review Required This PR is considered risky and requires Meta to import it to do more testing before merging. label Nov 28, 2026
@github-actions

github-actions Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

Visual regression

No visual differences at 1ff8f285bd30.

android: no capture

@ide
ide force-pushed the @ide/ios-single-line-input-line-height branch from 9fee22b to 6d92a69 Compare November 29, 2026 12:02
@ide
ide marked this pull request as ready for review November 29, 2026 13:49
@facebook-github-tools facebook-github-tools Bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Nov 29, 2026

@chrfalch chrfalch left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The approach looks right to me, and the new tests fail without the fix. (Commenting rather than approving since it's still a draft.) Two cases I'd like to see covered before this lands:

1. lineHeight smaller than the font. For example a 20pt font with lineHeight: 14. Before, maximumLineHeight compressed the line to 14pt. With the line height now omitted from the single-line field's attributed text, the field draws the font's full natural line in a shorter text rect, so ascenders or descenders may be clipped. Could you add a test for this case, or check it by hand and include a screenshot?

2. The state round trip while typing. Each keystroke sends the text back through _updateState, where the omitted line height has to be restored. The new comparison in _setAttributedString then has to treat the restored text as equal to what the field shows. No test drives that path, and the test plan doesn't mention typing. If the restore is off, the comparison fails and the text gets reset on every state update while the user types. A state-driven test, or at least a manual typing check (including with a lineHeight set on nested text), would close this.

ide added a commit to expo/react-native that referenced this pull request Dec 1, 2026
@ide
ide force-pushed the @ide/ios-single-line-input-line-height branch from 6d92a69 to 6cb33ab Compare December 1, 2026 19:00
@ide ide closed this Dec 1, 2026
@ide ide reopened this Dec 1, 2026

@chrfalch chrfalch left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the update, this answers both of my questions:

  1. Small lineHeight: the single-line field now draws without the line height and keeps the text centered, and testSingleLineInputCentersTextOnTheFieldAtAnyLineHeight checks the rendered position, including a line height smaller than the font.
  2. Typing round trip: the tests type through insertText: / textInputDidChange, echo the state back and assert the text isn't set again, for plain text, a nested run and the uncontrolled write-back.

A few optional follow-ups, not blocking:

  • Switching multiline while the field holds text isn't tested (you mention this too). Reading _setMultiline, going single-line → multi-line copies the drawn text, which has no line height and still carries the RCTOmittedLineHeight attribute, so the state may briefly measure short. A test that toggles multiline with text in the field would settle it.
  • _defaultTextAttributesFor: reads like a getter but also sets _omittedLineHeight. Splitting it or renaming it would make that visible.
  • The long ivar comment repeats much of the PR description. The CSS link plus one sentence on why the single-line field draws without the line height would be enough.

Looks good to me.

ide added a commit to expo/react-native that referenced this pull request Dec 1, 2026
@ide
ide force-pushed the @ide/ios-single-line-input-line-height branch from 6cb33ab to 39a0d2e Compare December 1, 2026 21:23
@ide

ide commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

@chrfalch Thanks. I took all three. While checking the first one I found a defect in the approved commit, so the update is larger than the notes. Could you take another look?

Switching multiline. Two tests now switch it in both directions on a view that holds the input's own text and a nested run. RCTCopyBackedTextInput copies a plain string, as on main, so neither the line heights nor the record cross the switch, and the next state sets the attributes again. Nothing writes the state at the switch, so it isn't measured short. An app doesn't reach this path with text: <TextInput> renders a different native component for each mode, so React mounts a new view. On the simulator _setMultiline: only ran on empty views, and no onChange fired.

The defect. With lineHeight: 40 and a value wider than the field, the field drew two lines when not being edited: a UITextField wraps under React Native's paragraph style, and without the line height two lines of the font fit. The drawn style now truncates where it would wrap, so the value is one centered line ending in an ellipsis, as in an input with no lineHeight (screenshot in the description). So that the state still gets the input's text exactly, the private attribute now records the whole replaced paragraph style instead of its line height, and the ivar holds the input's paragraph style. testSingleLineInputDrawsValueWiderThanTheFieldOnOneLine covers it.

_defaultTextAttributesFor: is now _updateDefaultTextAttributesWithProps:, which sets the field's default attributes and the ivar. Both call sites use it, so a content size category change now keeps the event emitter attribute, which main drops there.

The ivar comment is down to the CSS rule and the reason.

A single-line `TextInput` whose style gives its text a paragraph style draws the text wrong in two ways. Both come from the paragraph style that `RCTNSTextAttributesFromTextAttributes` builds, which replaces the one in `UITextField`'s default text attributes.

With a `lineHeight` larger than its font, the input draws its value, its placeholder and typed text at the bottom of the line instead of in the middle. `lineHeight` reaches the field as the paragraph style's minimum and maximum line height, and TextKit puts all of the extra leading above the glyphs: a 16 pt system font in a 24 pt line gets about 5 pt of extra leading, all of it above. CSS splits the leading equally above and below the glyphs (https://www.w3.org/TR/CSS2/visudet.html#leading).

With `textAlign`, `writingDirection`, `lineBreakStrategyIOS`, `lineHeight` or `lineBreakModeIOS="wordWrapping"`, on the input or on a nested `<Text>`, the input wraps a value wider than the field when it is not being edited (react#29068). The first line ends at a word with no ellipsis, and when the field is tall enough for two lines of its font, such as a 16 pt font in a 44 pt field, the second line is drawn too, across the field's bottom border. The placeholder loses its ellipsis the same way. The paragraph style is built from a new `NSMutableParagraphStyle`, whose line break mode is `NSLineBreakByWordWrapping` (https://developer.apple.com/documentation/uikit/nsparagraphstyle/default), where `UITextField`'s own is `NSLineBreakByTruncatingTail`, and a text field that is not being edited lays out its attributed text as the paragraph style says.

A single-line field now draws with paragraph styles that have neither. `NSLineBreakByWordWrapping` and `NSLineBreakByCharWrapping` become `NSLineBreakByTruncatingTail`. When the input has a `lineHeight`, the line height is removed: layout already makes the field as tall as its line, and `UITextField` centers the font's own line in its text rect. Both changes are made in the field's default text attributes, which also style the placeholder and typed text, and in the attributed text the field is given. Multi-line inputs are unchanged. The two changes depend on each other: without the line height, two lines of the font fit in a field that held one 40 pt line, so centering alone would draw a second line where `main` only loses the ellipsis.

`lineBreakModeIOS` was added (react#46128) for the same wrap (react#44107), and `lineBreakModeIOS="tail"` beside `textAlign` avoids it. A single-line input no longer needs it. The prop still chooses clipping or an ellipsis at the head, middle or tail, which keep to one line and are left alone. Its two wrapping values, `"wordWrapping"` and `"char"`, now end the line with an ellipsis in a single-line input. They never applied to the text being edited, which scrolls on one line.

A `lineHeight` smaller than the font's line is drawn the same way. The field is then shorter than its text, which is centered on the field and extends equally above and below it. Chrome and Safari center the text of an `<input>` that is shorter than its line the same way, and clip it to the box. The field clips the placeholder and the text being edited, but not a value that is not being edited, which can overlap the views next to the field. Before, the compressed line put the text above the field, and the text being edited lost its upper part.

Layout measures the field from the attributed string in the state, so that string keeps its line heights. A private attribute records the paragraph style replaced in each range where it is not the input's own, such as a nested `<Text>` with its own `lineHeight`, and the text written back to the state gets each recorded style again. Text with no record, which is the input's own text and text typed into the field, gets the input's paragraph style. The record holds the whole style because a nested `<Text>` can differ from the input in more than its line height, such as its alignment. The line break mode is not restored: the text written to the state has the truncating styles, and layout measures a single-line input at unlimited width (`BaseTextInputShadowNode::getTextConstraints`), where wrapping and truncating give the same size. `_setAttributedString` replaces the line break modes of a state's text and restores the line heights of the field's text before it compares the two, so a state that repeats the field's text leaves the field alone.

`updateProps:` and a change of the content size category set the default text attributes through one method, so a change of the content size category also keeps the event emitter attribute.
ide added a commit to expo/react-native that referenced this pull request Dec 2, 2026
@ide ide changed the title Center a single-line iOS TextInput's text within its lineHeight Center a single-line iOS TextInput's text and keep it on one line Dec 2, 2026
@ide
ide force-pushed the @ide/ios-single-line-input-line-height branch from 39a0d2e to 1ff8f28 Compare December 2, 2026 02:07
@ide

ide commented Dec 2, 2026

Copy link
Copy Markdown
Contributor Author

@chrfalch Before you look again: this PR now also fixes #29068.

What it is. A single-line TextInput with textAlign, writingDirection, lineBreakStrategyIOS or lineHeight wraps a value wider than the field when it isn't being edited. The paragraph style React Native builds wraps by default, where UITextField's own ends the line with an ellipsis.

Why in this PR. It's the same field and the same paragraph style. The last update fixed the wrap only for an input with a lineHeight, where removing the line height lets a second line fit, and listed the other props as a limitation. Those are #29068.

What changed since 39a0d2effd.

  • RCTSingleLineParagraphStyle replaces a wrapping line break mode with NSLineBreakByTruncatingTail. A single-line input applies it to its default text attributes and to the state's text, with or without a lineHeight.
  • RCTParagraphStyleByOmittingLineHeight now only removes the line height.
  • The state's text keeps the truncating mode. Layout measures a single-line input at unlimited width, so its size is the same. The line heights are restored as before.
  • lineBreakModeIOS="wordWrapping" and "char" no longer wrap a single-line input. The other four values are unchanged.
  • Tests: one draws the field and counts its lines for textAlign, a nested run and lineHeight; one checks the default attributes across a multiline change and a content size category change; one types into a field with textAlign.

The description has screenshots of each prop on main and on this branch.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Meta Review Required This PR is considered risky and requires Meta to import it to do more testing before merging. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TextInput with TextAlign on iOS does not add ellipsis instead wraps

2 participants