Refactor adjustSpannableFontToFit
to fix adjustsFontSizeToFit
Text prop on Android
#48915
+129
−81
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:
The
adjustSpannableFontToFit
method had a miscalculation that caused incorrect font sizes to be applied toText
elements on Android whenadjustsFontSizeToFit
was set totrue
. Specifically, the applied font size incorrectly depended on the initialfontSize
of theText
component rather than properly adjusting to fit the container.In the screenshot below, the left side illustrates the issue. Three
Text
components display the same text:"This line must be the same size as the other ones with the same text."
Each of these components has a
fontSize
value that would render the text larger than its container. With theadjustsFontSizeToFit
prop enabled, the font size should adjust so the text fits within a single line.However, due to the incorrect calculation, the font size depended on the initially applied
fontSize
, leading to inconsistent behavior.The issue has been fixed, as shown on the right side of the screenshot. Now, the font size adjusts correctly to ensure the text fits the container, regardless of the initial
fontSize
.In addition to resolving the issue, a binary search algorithm has been implemented to improve performance when determining the appropriate font size.
Changelog:
[ANDROID] [FIXED] - Fixed
adjustsFontSizeToFit
on Android sometimes applying wrong fontSizeTest Plan:
The screenshot above demonstrates the changes.