Repository navigation
[Android] Fix adjustsFontSizeToFit layout keeping the unshrunk font metrics - #59690
Conversation
…rics createLayout measured BoringLayout metrics before adjustSpannableFontToFit shrank the spans and paint, then built the final layout with those metrics. A single-line label shrunk to fit a height got the line height and baseline of its original font size. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Hi @kayahickindev! Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
|
Could a maintainer approve the workflow runs? @Abbondanzo, could you review this, or point me to the right person for Android text layout? It touches the same Verified locally on this branch, and again with the commit on top of main at 2cf3a3c (applies cleanly):
This also reproduces on 0.89-stable (74d7bda), where One behavior change beyond the description: text that Not run: the full ReactAndroid unit suite, iOS, and a physical device. Font scale 1.0 (left: without the re-measure, right: with it): Font scale 1.3 (left: without the re-measure, right: with it): |
…eight Three single-line labels that fit their container's width at full font size but are too tall for its 40dp height. Each should be fully visible and vertically centered once adjustsFontSizeToFit shrinks it. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Visual regressionNo visual differences at |
|
@Abbondanzo has imported this pull request. If you are a Meta employee, you can view this in D133602737. |
|
This pull request was successfully merged by @kayahickindev in 17579b5 When will my fix make it into a release? | How to file a pick request? |


Summary:
When
adjustsFontSizeToFitshrinks a single line of text to fit its height,TextLayoutManager.createLayoutbuilds the final layout with theBoringLayout.Metricsit measured beforeadjustSpannableFontToFitshrank the spans and the paint. The layout gets the text at the fitted size but the line metrics of the original font. So the line is as tall as the unshrunk line, and the baseline sits where the larger font would put it.adjustSpannableFontToFitalready re-measuresboringon every iteration of its own loop, so the font size it settles on is correct. Only the final layout increateLayoutreuses the stale value. The bug needs two conditions together: the text is boring, and it already fits the width at its original size. Otherwise the staleboring.widthfails the fit check andcreateLayoutfalls through toStaticLayout, which reads the current spans.The effect is easiest to see with
enablePreparedTextLayout, becausePreparedLayoutTextViewdraws this layout directly. A label inside a fixed-height button, shrunk byadjustsFontSizeToFitat a large system font scale, draws about 2dp below center. The legacyReactTextViewbuilds its own layout when it draws, which hides the misplaced baseline, but the height reported to Yoga is still the unshrunk line height.The fix re-measures
boringafteradjustSpannableFontToFit, under the same rule the loop uses (only when the text was boring). To test it,createLayout(text, paint, attributedString, paragraphAttributes, ...)andCreateLayoutResultbecome@VisibleForTesting internal, matching the other test seams in this file. There is no public API change.Minimal repro, run in RNTester with
enablePreparedTextLayouton, which is the default on main (results in the Test Plan):Changelog:
[ANDROID] [FIXED] - Text shrunk by
adjustsFontSizeToFitis laid out with the fitted font's line metrics instead of the original font'sTest Plan:
Added
TextLayoutManagerAdjustFontSizeToFitMetricsTest(Robolectric, native graphics). It lays out "Done" at 40px in a width it fits, with anAT_MOSTheight of three quarters of its full-size line height andadjustsFontSizeToFiton. It asserts that the text shrinks, that the layout fits the height it was shrunk to, and that its baseline matches a fresh layout of the same text at the fitted size.Before the fix (the new test only, with the re-measure removed):
The shrunk text (font size 28) was laid out with the 54px line height of the original 40px font.
After the fix:
BUILD SUCCESSFUL. All 15 suites incom.facebook.react.views.text, 102 tests, pass, 0 failures. The fitted layout is a 38pxBoringLayout, identical to a fresh layout at font size 28.yarn format-check-kotlinpasses.RNTester on an API 35 arm64 emulator (1080x2400, 420dpi), default feature flags, with the commit on top of main at 2cf3a3c and the repro above added as a temporary example that is not part of this PR. Without the re-measure, the label is drawn at the baseline of the 48dp font, so it sits below the 40dp box and is clipped. With it, the label is centered. The result is the same at font scales 1.0 and 1.3.
Font scale 1.0 (left: without the re-measure, right: with it):
Font scale 1.3 (left: without the re-measure, right: with it):
This PR also adds an RNTester example on the Text screen, "adjustsFontSizeToFit shrinking to fit a height": three single-line labels at font sizes 40, 48 and 64 in 40dp boxes. With the fix each label is fully visible and centered, checked in light and dark mode on the same emulator. Prettier, ESLint and Flow pass for the example.
Not run: the full ReactAndroid unit test suite, iOS, and a physical device. The same misplacement was seen in an app built on 0.86.3 with
enablePreparedTextLayout: a shrunk label sat 6px (about 2dp) below center at font scale 1.3 and was centered without the flag.