Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Center a single-line iOS TextInput's text within its lineHeight - #60378

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

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, its placeholder and typed text 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 font in a 24 pt line is drawn 4 pt low. CSS splits the leading equally above and below the glyphs (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. With this change a single-line field draws without the line height. It is removed from:

  • the default text attributes, which also style the placeholder and typed text;
  • 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 stored in the state, so that string keeps its line heights and the field measures as before. Nested <Text> runs can have line heights of their own, so a private attribute records the line height removed from each range of the field's text, and the text written back to the state after an edit gets each range's own value again. Text typed into an empty field, or set by setTextAndSelection, has no record and gets the input's own line height. Both conversions are in RCTTextInputUtils.

When the input's text attributes change, the field gets new default text attributes, and UITextField applies them to all of its text, which replaces every attribute of every range, records included. Nested runs then have the input's line height, as they do on main after the same change.

When multiline changes, the default text attributes are computed again. The text needs no conversion: RCTCopyBackedTextInput gives the new backed view the plain string without attributes, and a UITextField applies its default text attributes to all of its text.

Line heights are omitted only when the input itself has a lineHeight. A single-line input without one whose nested <Text> sets a lineHeight draws as on main.

A baseline offset is not an alternative: the field editor ignores it for the text being edited, so only the placeholder would move.

Changelog:

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

Test Plan:

Ran:

node scripts/clang-format.js --check <the 5 changed files>   # exit 0
git diff --check upstream/main                              # clean

Run on this base (2026-11-28, 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
  • On this branch: 8 tests, 0 failures.
  • With RCTTextInputComponentView.mm from main and the rest kept: testSingleLineInputDrawsWithoutLineHeight fails (the minimum and maximum line height are 40, not 0). The other 7 pass.
  • With the multiline condition removed from the default text attributes update in updateProps:oldProps:: testInputThatBecomesMultilineDrawsWithLineHeight fails (0, not 40).

Tests, in React/Tests/Text:

  • RCTTextInputUtilsTest, testRestoringLineHeightGivesEachRunItsOwn: a string with runs of line height 24 and 40 has none after omitting, and 24 and 40 again after restoring with a default of 24.
  • RCTTextInputUtilsTest, testRestoringLineHeightGivesUnrecordedTextTheDefault: text without a record gets the default.
  • RCTTextInputUtilsTest, testOmittingRecordsNothingForTextWithoutALineHeight: text whose line height is already omitted, as setTextAndSelection builds it from the field's default attributes, gets no record and restores to the default.
  • RCTTextInputComponentViewTests, testSingleLineInputDrawsWithoutLineHeight: a single-line input with lineHeight: 40 has no minimum or maximum line height in the paragraph style of the backed field's default text attributes, and keeps 40 to write back to the state.
  • RCTTextInputComponentViewTests, testMultilineInputDrawsWithLineHeight: a multi-line input keeps 40. It passes on main and guards the multi-line branch.
  • RCTTextInputComponentViewTests, testInputThatBecomesMultilineDrawsWithLineHeight: a single-line input that becomes multi-line has 40 again.

The component view tests turn off font scaling in their props: unit tests run without an application, so RCTFontSizeMultiplier() is 0 there and would scale every line height to 0.

React/Tests belongs to the React-RCTFabric pod's Tests test spec, which RNTester's Podfile does not install. To run it, I added :testspecs => ['Tests'] to the React-RCTFabric pod in scripts/cocoapods/fabric.rb, narrowed the test spec's source_files to these two test files, and added an empty Swift file to the test spec so the bundle links the Swift runtime that RCTSwiftUI needs. None of that is part of this change. No test drives the component view's state: that the state receives the restored text, and that a change of multiline reports no edit, are covered only by reading the code. The tests build the view without a state or an event emitter.

Checked in RNTester (Debug, same simulator) with a local example that is not part of this change: 16 pt single-line inputs with a value and with a placeholder at lineHeight 24 and 40, <TextInput style={{lineHeight: 24}}><Text style={{lineHeight: 40}}>Hi</Text></TextInput>, and a multi-line input at lineHeight 40, each with a border and a guide line at its vertical center.

  • On main, the single-line text and placeholders sit at the bottom of their fields, and at lineHeight 40 the text crosses the bottom border.
  • On this branch they are centered on the guide line. Every field keeps its frame, including the input with the nested <Text>, which is as tall as its 40 pt run. The multi-line input draws as on main.

Not checked: typed text, the nested input's height after a character is typed, and that switching multiline on while the input has text does not fire onChange. I had no way to type into the simulator.

🤖 Generated with Claude Code

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, so a 16 pt font in a 24 pt line
is drawn 4 pt low. 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>
@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

Copy link
Copy Markdown

Visual regression

No visual differences at 9fee22bc9ef1.

android: no capture

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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant