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. |
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.
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 |
Summary:
Fixes #29068.
A single-line
TextInputwhose style gives its text a paragraph style draws the text wrong in two ways. Both come from the paragraph style thatRCTNSTextAttributesFromTextAttributesbuilds, which replaces the one inUITextField's default text attributes.Not centered. With a
lineHeightlarger than its font, the input draws its value and placeholder at the bottom of the line instead of in the middle; with a large enoughlineHeightthe text crosses the field's bottom border.lineHeightreaches 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,lineHeightorlineBreakModeIOS="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 newNSMutableParagraphStyle, whose line break mode isNSLineBreakByWordWrapping(NSParagraphStyle.default), whereUITextField's own isNSLineBreakByTruncatingTail, and a text field that is not being edited lays out its attributed text as the paragraph style says.mainon the left and this change on the right, for a value wider than the field withtextAlign: 'right'and withlineHeight: 40:A single-line field now draws with paragraph styles that have neither:
NSLineBreakByWordWrappingandNSLineBreakByCharWrappingbecomeNSLineBreakByTruncatingTail.lineHeight, the line height is removed. Layout already makes the field as tall as its line, andUITextFieldcenters 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
mainonly loses the ellipsis.lineBreakModeIOS(#46128) was added for the same wrap, reported in #44107, andlineBreakModeIOS="tail"besidetextAlignavoids 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
lineHeightsmaller 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>whoseheightis 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. Onmainthe compressed line puts the text above the field, and the text being edited loses its upper part. Keeping the compressed line for such alineHeightwould 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 itslineHeightchanges. 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._setAttributedStringreplaces 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, whichmaindrops there.Limitations:
lineHeightis smaller than its font's line stays as tall as thelineHeight, as onmain. Chrome and Safari ignore such aline-heightwhen they size an<input>with noheight, and make it as tall as the font's line.lineHeightwhose nested<Text>sets one still draws that text at the bottom of its line.<Text>whose paragraph style equals the input's has no record, so it follows the input when the input'slineHeightchanges. The next state from React corrects that by setting the field's text once, as onmain.<Text>with its ownlineHeight, 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 onmain.<Text style={{lineHeight: 0}}>in an input with alineHeighthas no record, so the text for the state gives it the input's line height and each state sets the field's text again.lineBreakModeIOSalone does not reach the field, as onmain:TextAttributes::operator==leaveslineBreakModeout, soupdateProps:sees no change.Changelog:
[IOS] [FIXED] - Center the text and placeholder of a single-line
TextInputvertically within itslineHeight[IOS] [FIXED] - Keep a single-line
TextInputwithtextAlignor 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 itTest Plan:
Xcode 27.1 and an iOS 27.0 simulator (iPhone 18 Pro). No device was used.
Unit tests
All pass. The tests in
React/Tests/Text:testSingleLineInputWithParagraphStyleDrawsWideTextOnOneLinerenders 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 withtextAlign: 'right', with a paragraph style that comes only from a nested run, and withlineHeight: 40. A multi-line input draws three. Onmainthe first two draw two lines; with the line height omitted and the state's styles not replaced, the third does.testOnlySingleLineInputReplacesWrappingLineBreakModereads 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 aftermultilinebecomes true, and truncating again after it becomes false.lineBreakModeIOS="head"staysNSLineBreakByTruncatingHead. Onmainthe single-line cases are wrapping.testSingleLineInputDrawsWithoutLineHeight,testMultilineInputDrawsWithLineHeightandtestInputThatBecomesMultilineDrawsWithLineHeightcheck the line height of the default text attributes. Onmainthe first fails (40, not 0).testSingleLineInputCentersTextOnTheFieldAtAnyLineHeightrenders 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 nolineHeight, withlineHeight: 40and withlineHeight: 10. Onmainthe text is 20.5 pt lower at 40 and 9.5 pt higher at 10.testInputWithTextThatBecomesMultilineKeepsNoOmittedLineHeightandtestInputWithTextThatBecomesSingleLineDrawsWithoutLineHeightchangemultilineon 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).testStateWrittenAfterTypingHasTheLineHeightAndLeavesTheFieldAlonetypes into an empty focused field withlineHeight: 40and reads the text_updateStatewrites 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).testStateEchoingTypedTextLeavesTheFieldAloneandtestStateEchoingTextTypedIntoNestedRunLeavesTheFieldAlonetype into a focused field with alineHeight, 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).testStateRepeatingTypedTextLeavesTheFieldAlonedoes the same for an input withtextAlign: 'right'and nolineHeight, with a controlled and an uncontrolled state.testChangingLineHeightChangesItForTheInputsOwnTextOnlychanges an input'slineHeightfrom 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.RCTTextInputUtilsTestchecks 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:
main'sRCTTextInputComponentView.mmlineHeightmain's attributesNSLineBreakByCharWrappingis keptmultilinealone does not recompute the attributesmultilinecopies the text with its attributesThe names are shortened:
EchoingTypedTextistestStateEchoingTypedTextLeavesTheFieldAlone, 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-RCTFabricTestsspec. To run it locally (not in this change): add:testspecs => ['Tests']inscripts/cocoapods/fabric.rb, narrow the spec'ssource_filesto these files, and add an empty Swift file forRCTSwiftUI's Swift runtime.RNTester on the simulator
RNTester (Debug) built from
mainand from this branch, with local Playground pages, driven by an XCUITest that types on the software keyboard and reads a readout of each field'sonChangetext,onSelectionChangerange andonLayoutheight.lineHeightbelow the font (font size 20 withlineHeight: 14, and 60 with 42, text "Ágjpqy"):mainlineHeight(about 30 pt at 60/42), not clippedmainon the left, this branch on the right; not focused, then 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 noheightthe content box is as tall as the font's line (71 px and 73 px) and nothing is clipped; withheight: 42pxthe text is centered and clipped equally above and below, as on this branch.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: 40uncontrolled; the same controlled byvalue;lineHeight: 24with a child<Text style={{lineHeight: 40}}>that holds the value from state; the same with fixed child text; and font size 20 withlineHeight: 14.mainlineHeight: 40, uncontrolledlineHeight: 40fields_setAttributedString)Typing into the uncontrolled
lineHeight: 40field,mainon the left and this branch on the right:With the restore removed from the comparison in
_setAttributedString(a build of thelineHeightchange alone), the field's text is set 80 times in the 54 keystrokes. The readout still shows the right text and cursor, because_setAttributedStringputs the cursor back, so only the count of sets shows the resets.A value wider than the field,
lineHeight: 40and font size 16, not focused (mainon the left, this branch on the right). Onmainthe 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 nolineHeight, draws it on both. While the value is edited it scrolls and is clipped at the field's edge with no ellipsis, as onmain.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.
mainis on the left in each image.mainletterSpacing: 1; nested<Text>with no styletextAlignleft, right or center;writingDirection: 'ltr';lineBreakStrategyIOS="standard";lineBreakModeIOS="wordWrapping"; nested<Text>withtextAlign: 'right'textAlignsaystextAlign: 'right'withlineBreakModeIOS="tail"textAlign: 'right', noheight(one line tall)textAlign: 'right', placeholder wider than the fieldlineHeight: 40textAlign: 'right',multilineAfter 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. Onmainboth 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.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 byvalue, a child<Text style={{textAlign: 'right'}}>holding the value from state, andtextAlign: 'center'sized by its text.mainThe four fields after the typing, with the readout above them:
Switching
multiline. A page togglesmultilineon the four inputs above (controlled, nested child, uncontrolled, and uncontrolled with nolineHeight), types a character before each switch, and reads the value andonChangecount of the first three.<TextInput>renders a different native component for each mode, so React mounts a new view on every switch, onmainand on this branch: the character typed into the uncontrolledlineHeight: 40input 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. NoonChangefires at a switch. The heights of thelineHeight: 40inputs are 42.33 single-line and 82.33 multi-line (two lines of 40) on both, and the multi-line lines are spaced by thelineHeight.With
lineHeight24 and 40 on single-line, nested-run and multi-line inputs that are not being edited: onmainsingle-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.