Repository navigation
Conversation
Visual regressionNo visual differences at android: no capture |
9fee22b to
6d92a69
Compare
chrfalch
left a comment
There was a problem hiding this comment.
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.
6d92a69 to
6cb33ab
Compare
chrfalch
left a comment
There was a problem hiding this comment.
Thanks for the update, this answers both of my questions:
- Small
lineHeight: the single-line field now draws without the line height and keeps the text centered, andtestSingleLineInputCentersTextOnTheFieldAtAnyLineHeightchecks the rendered position, including a line height smaller than the font. - 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
multilinewhile 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 theRCTOmittedLineHeightattribute, so the state may briefly measure short. A test that togglesmultilinewith 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.
6cb33ab to
39a0d2e
Compare
|
@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 The defect. With
The ivar comment is down to the CSS rule and the reason. |
39a0d2e to
1ff8f28
Compare
|
@chrfalch Before you look again: this PR now also fixes #29068. What it is. A single-line 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 What changed since
The description has screenshots of each prop on |
chrfalch
left a comment
There was a problem hiding this comment.
Thanks for taking all three, and for catching the two-line case.
- The
multilineswitch tests, the_updateDefaultTextAttributesWithProps:rename and the shorter ivar comment all look good. - Folding #29068 in makes sense to me: once the line height is removed a second line of the font fits in the field, so the centering fix needs the truncation anyway. The round trip compares like with like (
_setAttributedStringapplies the single-line style to the incoming state before comparing), and since a single-line input is measured at unlimited width, truncating instead of wrapping doesn't change its size.
Two small requests:
- Please name both fixes in the PR title.
lineBreakModeIOS="wordWrapping"/"char"no longer wrapping a single-line input is a visible change to a public prop, and the title is what people scan. - As you note, the
React-RCTFabrictest spec isn't installed in RNTester, so these tests don't run in CI. Could you say that in the test plan, or open an issue to get them running? Otherwise they don't protect against regressions after merge.
Optional nit: the comment blocks in _setAttributedString could be trimmed the same way as the ivar comment.
Looks good to me.
1ff8f28 to
73b42e5
Compare
|
Done in 73b42e5:
|
b19ed39 to
113c4b8
Compare
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. Signed-off-by: James Ide <ide@expo.dev>
113c4b8 to
a956b1b
Compare
Summary:
Fixes #29068.
A single-line iOS
TextInputcan draw text below the center of the field or wrap it onto a second line. Both problems come from the paragraph style that React Native builds to replace the one inUITextField's default text attributes.Vertical alignment. React Native sets the paragraph style's minimum and maximum line heights to
lineHeight. TextKit puts all the extra leading above the glyphs, so a largelineHeightcan push the value or placeholder across the bottom border. CSS divides the leading equally above and below the glyphs. CSS 2, "Leading and half-leading"Wrapping. A paragraph style created for
textAlign,writingDirection,lineBreakStrategyIOSorlineHeightcan give the field the defaultNSLineBreakByWordWrappingmode in place ofUITextField'sNSLineBreakByTruncatingTail. A style from a nested<Text>run or an explicitlineBreakModeIOS="wordWrapping"can also introduce wrapping. A wide value loses its ellipsis when the field is unfocused and can draw a second line if the field is tall enough. A wide placeholder also loses its ellipsis. UIKit's default paragraph styleThe comparison shows an unfocused value wider than the field with
textAlign: "right"and withlineHeight: 40. The left side shows the baseline and the right side shows the fix.A single-line field converts word and character wrapping to tail truncation in its default text attributes and in the attributed text it receives from state. An input with a paragraph style gets an ellipsis by default.
lineBreakModeIOSstill accepts clipping and head, middle and tail truncation. Edited text scrolls on one line.When the input sets a
lineHeight, the field draws using the font's natural line height. Layout still uses the requested line height, andUITextFieldcenters the rendered line in the space layout gives it. Truncation matters here because the space that holds one 40 pt line can hold two lines of a 16 pt font. Both changes apply to single-line fields.For a
lineHeightbelow the font's natural line height, the field still centers the text. UIKit clips the placeholder and the focused value at the field's edges. An unfocused value can extend above and below the field and overlap neighboring views. Chrome and Safari also center the text when an HTML input's explicit height is below its font's line height, and both clip it to the field.The attributed string in state retains line heights for layout. When a nested
<Text>run has a different paragraph style with a positive line height, the field records the whole style so it can restore properties such as alignment as well as line height. When the field writes text back to state, it restores the input's current style for runs without a record. It keeps the normalized line break modes because single-line measurement uses unlimited width, where wrapping and truncation produce the same size. Before comparing incoming state with the field's text,_setAttributedStringnormalizes the incoming line break modes and restores the field's line heights. A state update that echoes what the field displays can then leave its text and selection alone.updateProps:and content size category changes use the same method to set default text attributes. That method carries forward the event emitter attribute on both paths.Limitations
lineHeightis below the font's line is still sized from the requested line height. Chrome and Safari size an HTML input without an explicit height to the font's line.<Text>with its own line height still draws at the bottom of its line when the single-line input itself has nolineHeight.<Text>whose paragraph style matches the input's follows the input when itslineHeightchanges. The next React state restores the nested line height by setting the field's text once, as on the baseline.<Text>has its ownlineHeight, the first character typed into an empty field or after a tap moves the cursor gets the input's line height. A controlled input's next state restores the nested line height by setting the field's text once, as on the baseline.<Text style={{lineHeight: 0}}>in an input with alineHeightgets the input's line height when text is written to state. Each state echo sets the field's text again.Changelog:
[IOS] [FIXED] - Center the text and placeholder of a single-line
TextInputvertically within itslineHeight[IOS] [FIXED] - Truncate wide text in single-line
TextInputfields with paragraph styles, includinglineBreakModeIOS="wordWrapping"andlineBreakModeIOS="char"Test Plan:
Xcode 27.1, iOS 27.0 Simulator, iPhone 18 Pro.
Unit tests
The RNTester workspace needs the
React-RCTFabricpod'sTestsspec for this command. Locally, enable it with:testspecs => ['Tests']inscripts/cocoapods/fabric.rb. InReact-RCTFabric.podspec, sets.swift_version = "5.0"and restrict the test spec toRCTTextInputComponentViewTests.mm,RCTTextInputUtilsTest.mmand an empty Swift file. The empty file links the Swift runtime thatRCTSwiftUIuses.From the repository root, set
SIMULATOR_UDIDto the iPhone 18 Pro simulator's identifier.Both suites pass on the rebased commit.
RCTTextInputComponentViewTestschecks that wide single-line values draw on one line with alignment, a nested paragraph style or a custom line height. It measures vertical position with line heights above and below the font's natural line height. It also checks placeholder and default styles, including after a content size category change, and switches between single-line and multiline modes.testChangeOfLineBreakModeAloneSetsDefaultTextAttributescovers an explicit head-to-middle change and clearing the style after recycling. Compare lineBreakMode in TextAttributes::operator== #60455 adds this test and makesTextAttributes::operator==compare the line break mode, so a change oflineBreakModeIOSreaches the field on its own.RCTTextInputUtilsTestchecks normalization of wrapping modes, preservation of clipping and explicit truncation modes, and restoration of paragraph styles for individual runs.Running the suites with each change below produced only the listed failures.
main'sRCTTextInputComponentView.mmlineHeightmain's attributesNSLineBreakByCharWrappingis keptmultilinealone does not recompute the attributesmultilinecopies the text with its attributesThe names are shortened:
EchoingTypedTextistestStateEchoingTypedTextLeavesTheFieldAlone, and so on.RNTester on the simulator
The RNTester XCUITest passes on the rebased commit and its
mainparent. The runs reproduce the rendering comparisons and confirm the typing, cursor and field-height readouts.A local Playground and an XCUITest exercise the software keyboard in RNTester (Debug). The Playground records each field's
onChangetext,onSelectionChangerange andonLayoutheight. The screenshots, browser comparisons and counts from instrumented builds below come from the original runs.Short line heights
The fields use the text "Ágjpqy", with font size 20 and
lineHeight: 14or font size 60 andlineHeight: 42.mainlineHeight(about 30 pt at 60/42), not clippedThe first comparison shows unfocused fields. The second shows the focused 60/42 field. Both show the baseline on the left.
The browser comparison uses
<input style="font-size: 60px; line-height: 42px">in headless Chrome 159.0.8154.6 and Safari on iOS 27.0. Without an explicit height, the content box fits the font's line at 71 px in Chrome and 73 px in Safari. Withheight: 42px, both browsers center the text and clip it equally above and below.Typing with a custom line height
The test types "hello world", taps inside the text and types "XY" in four fields. The fields use
lineHeight: 40with and without a controlled value,lineHeight: 24with a controlled child<Text style={{lineHeight: 40}}>, and font size 20 withlineHeight: 14. In a fifth field with fixed child text, the test taps inside the existing value and types "XY".mainlineHeight: 40, uncontrolledlineHeight: 40fields_setAttributedString)The recording shows typing into the uncontrolled
lineHeight: 40field, with the baseline on the left.A build of the line-height change with restoration disabled during
_setAttributedStringcomparison set the field's text 80 times over 54 keystrokes. The text and cursor still matched because_setAttributedStringrestored the cursor after each set.Wide values and placeholders
The unfocused field uses font size 16 and
lineHeight: 40. On the left, the baseline draws one line across the bottom border and cuts it at a word. On the right, the field centers the line and ends it with an ellipsis. During editing, the value scrolls and clips at the field's edge in both builds.The paragraph style comparison uses unfocused single-line inputs 280 pt wide and 44 pt tall with a 16 pt font and the value "one two three … twelve". The baseline is on the left.
mainletterSpacing: 1, or nested<Text>with no styletextAlignleft, right or center,writingDirection: 'ltr',lineBreakStrategyIOS="standard",lineBreakModeIOS="wordWrapping", or nested<Text>withtextAlign: 'right'textAlignsaystextAlign: 'right'withlineBreakModeIOS="tail"textAlign: 'right', noheight(one line tall)textAlign: 'right', placeholder wider than the fieldlineHeight: 40textAlign: 'right',multilineThe test also types into empty and controlled right-aligned fields, then moves focus away. Both values wrap after blur on the baseline. With the fix, each shows one line with an ellipsis after blur. A focused field scrolls on one line in both builds.
Typing with alignment
The test types "hello world", taps inside the text and types "XY" in a right-aligned uncontrolled field, a right-aligned controlled field, a field with a right-aligned child
<Text>, and a centered field sized by its text.mainThe comparison shows the four fields and their readouts after typing.
Switching between single-line and multiline modes
The test switches
multilineon controlled, nested, and uncontrolled fields, with an uncontrolled field withoutlineHeightas a comparison. It types before each switch and observes the value andonChangecount. React mounts a new native view at each switch in both builds. The uncontrolled field returns to its default value, controlled values persist, and switching fires noonChange. ThelineHeight: 40fields measure 42.33 pt in single-line mode and 82.33 pt for two lines in multiline mode.At line heights of 24 and 40, the fix centers unfocused single-line text within the same frames. Multiline text keeps its original spacing.
Dictation, marked text and right-to-left layout are outside these checks. The plain-text comparison for dictation and marked text still follows
main.