Skip to content

[Android] Fix adjustsFontSizeToFit shrinking text that fits and resizing nested font sizes - #60135

Open
kayahickindev wants to merge 1 commit into
react:mainfrom
kayahickindev:android-text-adjust-font-size-nested-spans
Open

kayahickindev wants to merge 1 commit into
react:mainfrom
kayahickindev:android-text-adjust-font-size-nested-spans

Conversation

@kayahickindev

Copy link
Copy Markdown

Summary:

With adjustsFontSizeToFit, Android changes font sizes it should leave alone:

  • Text that already fits can come back smaller, which is [Android] Using adjustsFontSizeToFit=true sometimes causes font size to be too small. #56799. In a unit test at density 1, 30 of the pixel sizes from 8 to 120 shrink, for example 22px to 17px and 54px to 48px.
  • A nested Text with a smaller fontSize changes size even when nothing needs to shrink. With a 40px "Total " and a 10px "USD" that already fit, "USD" comes back at 8px. With minimumFontScale={0.5} it comes back at 24px.
  • Text that has to shrink lands below the size that fits. A 40px and 20px pair in a box that fits them at exactly half size ends at 17px and 8px instead of 20px and 10px.

TextLayoutManager.adjustSpannableFontToFit binary-searches the largest font size. On each probe it multiplies the paint and every ReactAbsoluteSizeSpan by currentFontSize / previousFontSize and truncates with toInt(). The sizes it multiplies were already truncated by the previous probe, so the error compounds. In 32-bit floats 22 * (13f / 22f) is 12.999999, which truncates to 12, and the search never gets the lost pixel back, even when it ends at the original size. Every span is also clamped to the minimum font size on each probe, so a nested 10px run is raised to the 20px minimum on a probe below the original size, and the later probes on the way back up scale it from there to 24px.

The fix keeps the original span sizes and paint text size, and on each probe computes every size from its original:

  • Text that fits ends on the original size, where every span and the paint keep their original sizes exactly.
  • Text that has to shrink scales every size by the same ratio, rounds it, and clamps it to the minimum font size, as Android did before.

The shrink rule matches iOS. In packages/react-native/Libraries/Text/Text/NSTextStorage+FontScaling.m, scaleFontSizeToFitSize:minimumFontSize:maximumFontSize: returns without scaling when the text already fits ("Don't scale the font if it already fits"). Otherwise it restores the original string before each try, and scaleFontSizeWithRatio:minimumFontSize:maximumFontSize: sets every run to MAX(MIN(font.pointSize * ratio, maximumFontSize), minimumFontSize). So a 40px and 10px pair shrunk to a 0.5 minimumFontScale ends at 20px and 20px on both platforms.

#56815 fixes the single-size case by returning early when the text fits at its original size. This change removes the truncation instead, which also covers text that has to shrink and mixed font sizes, without an extra layout. The two touch the same lines in adjustSpannableFontToFit.

This should fix #56799. I reproduced the shrinking in a unit test, but did not run that issue's reproducer on a device.

Minimal repro:

<View style={{width: 300}}>
  <Text numberOfLines={1} adjustsFontSizeToFit minimumFontScale={0.5} style={{fontSize: 40}}>
    Total <Text style={{fontSize: 10}}>USD</Text>
  </Text>
</View>

Changelog:

[ANDROID] [FIXED] - Fix adjustsFontSizeToFit shrinking Text that already fits and changing the size of nested Text

Test Plan:

Added five tests to TextLayoutManagerMinimumFontScaleTest (Robolectric):

  • text that already fits keeps its font size at every size: a single span at each size from 8 to 120px, in a box it fits.
  • smaller font sizes are kept when the text already fits: 40px and 10px spans that fit.
  • minimumFontScale does not grow smaller font sizes: the same with minimumFontScale 0.5.
  • smaller font sizes shrink in proportion to the largest one: 40px and 20px spans in the height of the same text at 20px and 10px, with minimumFontScale 0.1, using native graphics so the height follows the font metrics. Expects 20px and 10px.
  • shrunk font sizes are clamped to the minimum font size: 40px and 10px spans in a height they cannot fit, with minimumFontScale 0.5. Expects 20px and 20px, as on iOS. This one passes before and after the fix, and guards the clamp.
./gradlew :packages:react-native:ReactAndroid:testDebugUnitTest \
  --tests 'com.facebook.react.views.text.TextLayoutManagerMinimumFontScaleTest' \
  -Preact.internal.useHermesNightly=true -PreactNativeArchitectures=arm64-v8a

Before the fix (the TextLayoutManager.kt change reverted, tests kept), four tests fail and the other nine pass:

TextLayoutManagerMinimumFontScaleTest > smaller font sizes are kept when the text already fits FAILED
    Expecting actual: [40, 8] to contain exactly (and in same order): [40, 10]
TextLayoutManagerMinimumFontScaleTest > minimumFontScale does not grow smaller font sizes FAILED
    Expecting actual: [40, 24] to contain exactly (and in same order): [40, 10]
TextLayoutManagerMinimumFontScaleTest > text that already fits keeps its font size at every size FAILED
    Expecting empty but was: [22, 23, 30, 34, 49, 53, 54, 55, 56, 60, 61, 64, 72, 79, 80, 93, 94, 97, 98, 99, 100, 101, 103, 107, 109, 110, 112, 113, 114, 116]
TextLayoutManagerMinimumFontScaleTest > smaller font sizes shrink in proportion to the largest one FAILED
    Expecting actual: [17, 8] to contain exactly (and in same order): [20, 10]

13 tests completed, 4 failed

After the fix, the whole package:

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

BUILD SUCCESSFUL: 18 suites, 129 tests, 0 failures, including the existing minimumFontScale, minimumFontSize and 4dp floor tests and TextLayoutManagerFontSizeToFitPixelHeightTest. On main at f4f8b6a the same command runs 18 suites, 124 tests, 0 failures.

node ./scripts/format-kotlin.js --check (what yarn format-check-kotlin runs) passes, and compileDebugKotlin and compileDebugUnitTestKotlin pass with -PenableWarningsAsErrors=true.

Not run: a device or emulator, iOS, and the #56799 reproducer. The tests above are Robolectric unit tests at density 1 only.

🤖 Generated with Claude Code

…ing nested font sizes

adjustSpannableFontToFit rescaled every font size span by the ratio
between the current and the previous probe, truncating to whole pixels
each time. The truncation compounds across probes, so text that already
fit could come back smaller: a single 22px span ended at 17px, and 30 of
the sizes from 8 to 120px shrank. Nested font sizes drifted too: a 10px
run next to a 40px run that already fit came back as 8px, and with
minimumFontScale={0.5} the clamp to the minimum on the way down left it
at 24px.

Scale every span and the paint from its original size on each probe.
Text that fits ends on the original size and keeps every size exactly.
Text that has to shrink scales every size by the same ratio, rounds it,
and clamps it to the minimum font size, as scaleFontSizeWithRatio does
on iOS and as Android did before.

Changelog:
[ANDROID] [FIXED] - Fix adjustsFontSizeToFit shrinking Text that already fits and changing the size of nested Text

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@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 19, 2026
@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 19, 2026

This branch has not been deployed

No deployments
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. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Android] Using adjustsFontSizeToFit=true sometimes causes font size to be too small.

1 participant