From 79e8474b14e118daa0f6e525d6546892a09a09a3 Mon Sep 17 00:00:00 2001 From: g4rb4g3 Date: Thu, 22 Jun 2023 17:19:40 -0700 Subject: [PATCH] fix: int cast can cause endless loop if value < 1 (#38016) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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: https://github.com/facebook/react-native/pull/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 --- packages/react-native/Libraries/Text/Text.d.ts | 10 +++++----- .../facebook/react/views/text/ReactTextShadowNode.java | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/react-native/Libraries/Text/Text.d.ts b/packages/react-native/Libraries/Text/Text.d.ts index bee0c632372ea0..b90bf240ba72d9 100644 --- a/packages/react-native/Libraries/Text/Text.d.ts +++ b/packages/react-native/Libraries/Text/Text.d.ts @@ -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. @@ -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; } /** diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextShadowNode.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextShadowNode.java index b69452d9384fcf..a66950c7966af6 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextShadowNode.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextShadowNode.java @@ -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 =