Repository navigation
[iOS] Lay out multiline TextInput lines without the font's leading - #60086
Open
jkasprzyk17 wants to merge 1 commit into
Open
jkasprzyk17 wants to merge 1 commit into
jkasprzyk17 wants to merge 1 commit into
Conversation
React Native measures text with NSLayoutManager.usesFontLeading set to NO (since react#27195), so a multiline TextInput is sized for lines of ascender + descender. RCTUITextView kept UIKit's default and laid its lines out with the font's leading on top. For a font with leading, the text then didn't fit the measured view: with Hiragino Sans at 16 pt each line took 24 pt instead of 16 pt, the view scrolled, its first line was cut off and the selection highlight was a line and a half tall. An explicit lineHeight didn't prevent it. Turn the font's leading off in RCTUITextView too, on its NSTextLayoutManager, or on its NSLayoutManager where UIKit uses TextKit 1. Fonts without leading, such as the system font, lay out as before.
Visual regressionNo visual differences at android: no capture |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary:
Refs #59842 (fixes its multiline part; see "Not addressed" for the rest).
React Native measures iOS text with
NSLayoutManager.usesFontLeading = NO, a choice that dates back to #27195. A multilineTextInputis therefore sized for lines of ascender + descender.RCTUITextViewkept UIKit's default and laid its lines out with the font's leading on top. Most fonts have no leading, but 62 of the 186 font families on macOS do, including every Hiragino family (8 pt at 16 pt, 0.5 em), Times New Roman, PT Sans, Geeza Pro and the Indic Sangam/Kohinoor families. For these fonts the text doesn't fit the view React Native measured:lineHeightdoesn't prevent any of this.This change turns the font's leading off in
RCTUITextViewas well, so the multiline input lays out its lines the way they were measured and the way<Text>draws them. On iOS 16+, UIKit backs the view with TextKit 2, so the property is set on itsNSTextLayoutManager; that property exists on TextKit 2, so the view doesn't fall back to TextKit 1. Where UIKit uses TextKit 1, it is set on itsNSLayoutManager. #45268 found the same missing setting in 2024; it was closed as a duplicate of #39145, which is a different bug (see below).Changelog:
[IOS] [FIXED] - Multiline TextInput no longer adds the font's leading to its lines, so fonts with leading such as Hiragino Sans fit the measured height
Test Plan:
RNTester on an iOS 26.5 simulator (iPhone 17 Pro), New Architecture, Debug. The Playground rendered, once in Hiragino Sans and once in the system font: a single-line
TextInputwith a value, one with a placeholder, a two-line<Text>, a two-line multilineTextInput, a<Text>withlineHeight: 24, and a multilineTextInputwithlineHeight: 24. I built the app with and without the change and relaunched it for each run.TextInput, Hiragino Sans 16 ptlineHeight(measured 37.33 pt)<Text>lineHeight: 24(measured 53.33 pt)node scripts/clang-format.js --checkon the changed file passes.Before / after, Hiragino Sans:
Selection after the change:
Interaction with #39145 / #56487
With an explicit
lineHeight,TextInputon iOS puts the glyphs at the bottom of each line instead of centering them as<Text>does. That is #39145, and #56487 addresses it. Onmainthis already shows for the system font: compare<Text>(top) with a multilineTextInput(bottom), both withlineHeight: 24:For Hiragino Sans, the 8 pt of leading used to hide that. After this change Hiragino behaves like the system font in this case, and since Hiragino's glyphs reach 1.46 pt below its declared descent, the last line's descenders touch the bottom edge until #56487 centers the line. This change and #56487 touch different lines of
RCTUITextView.mm.Not addressed
Items 1 and 2 of #59842, descenders clipped in
<Text>and in single-lineTextInput, come from the font's metrics. Hiragino Sans declares a descent of 1.92 pt at 16 pt, butg j p q yreach 3.38 pt below the baseline. With the no-leading layout from #27195, the glyphs' ink falls outside the line box. Changing that would change the height of<Text>for every font with leading, so it is out of scope here.Not tested: a physical device, and the TextKit 1 branch (iOS 15, or a
UITextViewthat UIKit has switched to TextKit 1).