Skip to content

Center a single-line iOS TextInput's text within its lineHeight - #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:

A single-line TextInput whose lineHeight is larger than its font 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").

Layout already makes a single-line field as tall as its line, and UITextField centers the font's own line in its text rect. So when the input has a lineHeight, a single-line field now draws without it: it is removed from the default text attributes (which also style the placeholder) and from the field's attributed text. This applies to any lineHeight on a single-line input, including one smaller than the font's line. Multi-line fields keep it. A baseline offset would not work: the field editor ignores it for the text being edited.

The state's attributed string keeps its line heights, so measurement is unchanged. A private attribute records the line height removed from each range, so text written back to the state gets each nested <Text> run's value again; text with no record gets the input's.

Limitations:

  • A single-line input without lineHeight whose nested <Text> sets one draws as before.
  • When the input's text attributes change, UITextField applies the new defaults to all of its text, records included, so nested runs get the input's line height, as on main.

Changelog:

[IOS] [FIXED] - Center the text and placeholder of a single-line TextInput vertically within its lineHeight

Test Plan:

Xcode 27.1, iOS 27.0 simulator (iPhone 18 Pro):

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

New tests in React/Tests/Text: RCTTextInputUtilsTest (omitting and restoring per-run line heights) and RCTTextInputComponentViewTests (single-line drops the line height from the field but not the state; multi-line keeps it, which passes on main; becoming multi-line restores it). On main, testSingleLineInputDrawsWithoutLineHeight fails (40, not 0); without the multiline condition in updateProps:oldProps:, testInputThatBecomesMultilineDrawsWithLineHeight fails (0, not 40). Font scaling is off in these tests: without an application RCTFontSizeMultiplier() is 0.

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.

In RNTester, with a local example of single-line, nested-run and multi-line inputs at lineHeight 24 and 40: 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: typed text, the nested input's height after typing, that switching multiline with text does not fire onChange, and that the state receives the restored text (the tests build the view without a state).

@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 6d92a696143c.

android: no capture

A single-line `TextInput` whose `lineHeight` is larger than its font
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).

Layout already makes a single-line field as tall as its line, and
`UITextField` centers the font's own line in its text rect. A
single-line field therefore draws without the line height: it is
removed from the default text attributes, which also style the
placeholder and typed text, and from the attributed text the field is
given. Multi-line fields keep the line height, which spaces their
lines.

Layout measures the field from the attributed string in the state, so
that string keeps its line heights. A private attribute records the
line height removed from each range of the field's text, and the text
written back to the state gets each range's own value again. Text typed
into an empty field has no record and gets the input's line height.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@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

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.

1 participant