Skip to content

Commit

Permalink
fix: int cast can cause endless loop if value < 1 (#38016)
Browse files Browse the repository at this point in the history
Summary:
I faced an issue that on Android the whole UI would freeze when using minimumFontScale. This is caused by an int cast that turns the while loop into an endless loop.
Also the docs are not correct since they say it is an iOS only prop.
https://reactnative.dev/docs/text#minimumfontscale-ios

## Changelog:

[ANDROID] [FIXED] - UI freezing when using minimumFontScale

Pull Request resolved: #38016

Test Plan:
Run this sample app with and without this fix. https://github.com/g4rb4g3/androidMinimumFontScaleBug
Without the ui will freeze when hitting the + button, with the fix a Text component will be shown and no freeze will happen. 🙂

Reviewed By: cipolleschi

Differential Revision: D46931439

Pulled By: NickGerleman

fbshipit-source-id: 6985443b3424539b40bc0081fe742ab59105a2ae
  • Loading branch information
g4rb4g3 authored and facebook-github-bot committed Jun 23, 2023
1 parent 95847fc commit 79e8474
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
10 changes: 5 additions & 5 deletions packages/react-native/Libraries/Text/Text.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,6 @@ export interface TextPropsIOS {
| 'largeTitle'
| undefined;

/**
* Specifies smallest possible scale a font can reach when adjustsFontSizeToFit is enabled. (values 0.01-1.0).
*/
minimumFontScale?: number | undefined;

/**
* When `true`, no visual change is made when text is pressed down. By
* default, a gray oval highlights the text on press down.
Expand Down Expand Up @@ -209,6 +204,11 @@ export interface TextProps
* - >= 1: sets the maxFontSizeMultiplier of this node to this value
*/
maxFontSizeMultiplier?: number | null | undefined;

/**
* Specifies smallest possible scale a font can reach when adjustsFontSizeToFit is enabled. (values 0.01-1.0).
*/
minimumFontScale?: number | undefined;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public long measure(
// TODO: We could probably use a smarter algorithm here. This will require 0(n)
// measurements
// based on the number of points the font size needs to be reduced by.
currentFontSize = currentFontSize - (int) PixelUtil.toPixelFromDIP(1);
currentFontSize -= Math.max(1, (int) PixelUtil.toPixelFromDIP(1));

float ratio = (float) currentFontSize / (float) initialFontSize;
ReactAbsoluteSizeSpan[] sizeSpans =
Expand Down

0 comments on commit 79e8474

Please sign in to comment.