From ed7766cee941e36af05b19a315b442edf738ef80 Mon Sep 17 00:00:00 2001 From: Jakub Piasecki Date: Fri, 19 Apr 2024 10:20:15 -0700 Subject: [PATCH] Don't enlarge font when `adjustFontSizeToFit` is set on iOS on the new architecture (#44163) Summary: On the old architecture `adjustFontSizeToFit` only shrinks the font size when there's too little space, while on the new arch it's also enlarged when there's too much space so that it always takes the entire width. This PR changes this behavior so that it only shrinks the text. Fixes https://github.com/facebook/react-native/issues/42044 bypass-github-export-checks ## Changelog: [IOS] [FIXED] - Fixed font size enlarging when `adjustFontSizeToFit` is set Pull Request resolved: https://github.com/facebook/react-native/pull/44163 Test Plan:
Tested on the following code ```jsx import { Text, SafeAreaView, View, StyleSheet } from 'react-native'; export default function Test() { return ( Some text that fits (no adjust, unlimited height) Some text that fits (adjust, unlimited height) Some text that fits (no adjust, 1 line) Some text that fits (adjust, 1 line) Some longer text that doesn't fit if displayed in one line (no adjust, unlimited height) Some longer text that doesn't fit if displayed in one line (adjust, unlimited height) Some longer text that doesn't fit if displayed in one line (no adjust, 1 line) Some longer text that doesn't fit if displayed in one line (adjust, 1 line) Even longer text that doesn't even fit if it has as much as two entire lines for itself, what a darn shame (no adjust, unlimited height) Even longer text that doesn't even fit if it has as much as two entire lines for itself, what a darn shame (adjust, unlimited height) Even longer text that doesn't even fit if it has as much as two entire lines for itself, what a darn shame (no adjust, 2 lines) Even longer text that doesn't even fit if it has as much as two entire lines for itself, what a darn shame (adjust, 2 lines) ); } ```
|Old arch (without this PR)|Old arch (with this PR)| |-|-| |old_no_change|old_changed| |New arch (without this PR)|New arch (with this PR)| |-|-| |new_no_change|new_changed| Reviewed By: cortinico Differential Revision: D56356139 Pulled By: cipolleschi fbshipit-source-id: d11a5f4b95fb7da28a24d9136d41349d39851d9e --- .../Libraries/Text/Text/NSTextStorage+FontScaling.m | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/react-native/Libraries/Text/Text/NSTextStorage+FontScaling.m b/packages/react-native/Libraries/Text/Text/NSTextStorage+FontScaling.m index ee391a20f6c..fa86a458970 100644 --- a/packages/react-native/Libraries/Text/Text/NSTextStorage+FontScaling.m +++ b/packages/react-native/Libraries/Text/Text/NSTextStorage+FontScaling.m @@ -19,6 +19,11 @@ typedef NS_OPTIONS(NSInteger, RCTTextSizeComparisonOptions) { minimumFontSize:(CGFloat)minimumFontSize maximumFontSize:(CGFloat)maximumFontSize { + // Don't scale the font if it already fits + if ([self compareToSize:size thresholdRatio:0.01] & RCTTextSizeComparisonSmaller) { + return; + } + CGFloat bottomRatio = 1.0 / 128.0; CGFloat topRatio = 128.0; CGFloat ratio = 1.0;