Skip to content

Keep adjustsFontSizeToFit from shrinking text at whole-pixel heights on Android - #59792

Open
Abbondanzo wants to merge 2 commits into
react:mainfrom
Abbondanzo:export-D129533186
Open

Abbondanzo wants to merge 2 commits into
react:mainfrom
Abbondanzo:export-D129533186

Conversation

@Abbondanzo

Copy link
Copy Markdown
Contributor

Summary:
adjustsFontSizeToFit shrinks the font while the text layout is taller than its AT_MOST height. That height arrives in pixels after Yoga subtracts the container's padding and converts it from DIPs back to pixels, all in 32-bit floats, so a whole number of pixels can come back a fraction short: 46px comes back as 45.999996px. A layout exactly as tall as the space it was given then counts as too tall. When the text sets a lineHeight, shrinking the font never makes it shorter, so it shrinks all the way to the minimum font size.

Compare the layout height against the AT_MOST height with the same 0.001px tolerance the previous change added for widths, now named PIXEL_SIZE_TOLERANCE.

Adds a unit test and an RNTester example. The example lays out 17 labels, each with a lineHeight of a whole number of pixels, in containers that leave exactly that height below their padding. Each row is a pixel taller than the last. Without this change, 11 of them shrink to the minimum font size at 320dpi and 4 at 420dpi.

Changelog:
[Android][Fixed] - Fix adjustsFontSizeToFit shrinking text that exactly fits its container's height

Differential Revision: D129533186

Summary:

A `Text` laid out at its own measured width is measured twice. This happens, for example, when it has `maxWidth: '100%'` inside a container that is only as wide as the text. The second measurement gets the first result back as its `AT_MOST` width, after Yoga adds and removes the container's padding and converts the width from pixels to DIPs and back, all in 32-bit floats. A whole number of pixels can come back a fraction short: at 420dpi, 372px comes back as 371.99997px. `TextLayoutManager.createLayout` floors `AT_MOST` widths, so the text gets a layout one pixel narrower than it was just measured to need and wraps onto a second line.

Floor `AT_MOST` widths with a 0.001px tolerance, so a width that falls a float error short of a whole pixel keeps that pixel. Widths further below a whole pixel still round down.

Adds a `createLayout` unit test and an RNTester example that lays out 17 labels at their own widths, each about a pixel wider than the last. Without this change, every label wraps at 320dpi and 6 of them wrap at 420dpi.

Changelog:
[Android][Fixed] - Fix `Text` wrapping onto a second line when it is laid out at its own measured width

Differential Revision: D129505599
…on Android

Summary:
`adjustsFontSizeToFit` shrinks the font while the text layout is taller than its `AT_MOST` height. That height arrives in pixels after Yoga subtracts the container's padding and converts it from DIPs back to pixels, all in 32-bit floats, so a whole number of pixels can come back a fraction short: 46px comes back as 45.999996px. A layout exactly as tall as the space it was given then counts as too tall. When the text sets a `lineHeight`, shrinking the font never makes it shorter, so it shrinks all the way to the minimum font size.

Compare the layout height against the `AT_MOST` height with the same 0.001px tolerance the previous change added for widths, now named `PIXEL_SIZE_TOLERANCE`.

Adds a unit test and an RNTester example. The example lays out 17 labels, each with a `lineHeight` of a whole number of pixels, in containers that leave exactly that height below their padding. Each row is a pixel taller than the last. Without this change, 11 of them shrink to the minimum font size at 320dpi and 4 at 420dpi.

Changelog:
[Android][Fixed] - Fix `adjustsFontSizeToFit` shrinking text that exactly fits its container's height

Differential Revision: D129533186
@meta-codesync

meta-codesync Bot commented Nov 4, 2026

Copy link
Copy Markdown

@Abbondanzo has exported this pull request. If you are a Meta employee, you can view the originating Diff in D129533186.

@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 4, 2026
@facebook-github-tools facebook-github-tools Bot added p: Facebook Partner: Facebook Partner labels Nov 4, 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. meta-exported p: Facebook Partner: Facebook Partner

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant