Skip to content

[Android] Fix adjustsFontSizeToFit layout keeping the unshrunk font metrics - #59690

Merged
meta-codesync[bot] merged 2 commits into
react:mainfrom
kayahickindev:fix/android-adjustsfontsizetofit-boring-metrics
Nov 24, 2026
Merged

meta-codesync[bot] merged 2 commits into
react:mainfrom
kayahickindev:fix/android-adjustsfontsizetofit-boring-metrics

Conversation

@kayahickindev

@kayahickindev kayahickindev commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Summary:

When adjustsFontSizeToFit shrinks a single line of text to fit its height, TextLayoutManager.createLayout builds the final layout with the BoringLayout.Metrics it measured before adjustSpannableFontToFit shrank 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.

adjustSpannableFontToFit already re-measures boring on every iteration of its own loop, so the font size it settles on is correct. Only the final layout in createLayout reuses 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 stale boring.width fails the fit check and createLayout falls through to StaticLayout, which reads the current spans.

The effect is easiest to see with enablePreparedTextLayout, because PreparedLayoutTextView draws this layout directly. A label inside a fixed-height button, shrunk by adjustsFontSizeToFit at a large system font scale, draws about 2dp below center. The legacy ReactTextView builds 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 boring after adjustSpannableFontToFit, under the same rule the loop uses (only when the text was boring). To test it, createLayout(text, paint, attributedString, paragraphAttributes, ...) and CreateLayoutResult become @VisibleForTesting internal, matching the other test seams in this file. There is no public API change.

Minimal repro, run in RNTester with enablePreparedTextLayout on, which is the default on main (results in the Test Plan):

<View style={{height: 40, justifyContent: 'center', backgroundColor: '#ddd'}}>
  <Text numberOfLines={1} adjustsFontSizeToFit style={{fontSize: 48}}>
    Done
  </Text>
</View>

Changelog:

[ANDROID] [FIXED] - Text shrunk by adjustsFontSizeToFit is laid out with the fitted font's line metrics instead of the original font's

Test Plan:

Added TextLayoutManagerAdjustFontSizeToFitMetricsTest (Robolectric, native graphics). It lays out "Done" at 40px in a width it fits, with an AT_MOST height of three quarters of its full-size line height and adjustsFontSizeToFit on. 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.

./gradlew :packages:react-native:ReactAndroid:testDebugUnitTest \
  --tests 'com.facebook.react.views.text.*' \
  -Preact.internal.useHermesNightly=true -PreactNativeArchitectures=arm64-v8a

Before the fix (the new test only, with the re-measure removed):

TextLayoutManagerAdjustFontSizeToFitMetricsTest > text shrunk to fit a height is laid out with the fitted font metrics FAILED
    java.lang.AssertionError:
    Expecting actual:
      54.0f
    to be less than or equal to:
      40.5f

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 in com.facebook.react.views.text, 102 tests, pass, 0 failures. The fitted layout is a 38px BoringLayout, identical to a fresh layout at font size 28.

yarn format-check-kotlin passes.

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):

RNTester before and after at font scale 1.0

Font scale 1.3 (left: without the re-measure, right: with it):

RNTester before and after at font scale 1.3

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.

…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>
@meta-cla

meta-cla Bot commented Oct 30, 2026

Copy link
Copy Markdown

Hi @kayahickindev!

Thank you for your pull request and welcome to our community.

Action Required

In 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.

Process

In 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 CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@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 18, 2026
@meta-cla

meta-cla Bot commented Nov 18, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@kayahickindev

kayahickindev commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor Author

Could a maintainer approve the workflow runs? Test All and Format are waiting on approval.

@Abbondanzo, could you review this, or point me to the right person for Android text layout? It touches the same adjustsFontSizeToFit path as #59792.

Verified locally on this branch, and again with the commit on top of main at 2cf3a3c (applies cleanly):

  • The new test fails without the re-measure (line height 54px against a 40.5px limit) and passes with it.
  • com.facebook.react.views.text.* passes: 102 tests on this branch, 125 on main.
  • yarn format-check-kotlin passes.
  • RNTester on an API 35 arm64 emulator (1080x2400, 420dpi), default flags, font scales 1.0 and 1.3, with the repro from the description. Before: the label is drawn at the baseline of the 48dp font, so it sits below the 40dp box and is clipped. After: it is centered. Screenshots below.

This also reproduces on 0.89-stable (74d7bda), where enablePreparedTextLayout is on by default. The commit cherry-picks cleanly there and the text tests pass (103).

One behavior change beyond the description: text that adjustsFontSizeToFit shrinks to fit an AT_MOST width is boring again after the re-measure, so it becomes a BoringLayout at its own width (571px in my probe) instead of a StaticLayout at the full available width (600px). Height and baseline are unchanged. This matches how text that needs no shrinking is measured. I have not checked it on a device.

Not run: the full ReactAndroid unit suite, iOS, and a physical device.

Font scale 1.0 (left: without the re-measure, right: with it):

RNTester before and after at font scale 1.0

Font scale 1.3 (left: without the re-measure, right: with it):

RNTester before and after at font scale 1.3

@facebook-github-tools facebook-github-tools Bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Nov 18, 2026
…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>
@github-actions

github-actions Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Visual regression

No visual differences at 8e8f77a46e9f.

@meta-codesync

meta-codesync Bot commented Nov 20, 2026

Copy link
Copy Markdown

@Abbondanzo has imported this pull request. If you are a Meta employee, you can view this in D133602737.

@meta-codesync
meta-codesync Bot merged commit 17579b5 into react:main Nov 24, 2026
178 of 179 checks passed
@react-native-bot

Copy link
Copy Markdown
Collaborator

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?

@react-native-bot react-native-bot added the Merged This PR has been merged. label Nov 24, 2026
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. Merged This PR has been merged. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. visual-change-approved

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants