From bc04bb40723d48549a39fe4d580f71cf694f12c7 Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Wed, 20 Nov 2024 09:25:48 -0800 Subject: [PATCH] Fabric: Adjusts the weight according to the font name (#47742) Summary: Fixes another font weight issue mentioned in https://github.com/facebook/react-native/issues/47656#issuecomment-2486282496. We can get the weight from font name if user not specify weight. esbenvb Is this work for you ? ## Changelog: [IOS] [FIXED] - Fabric: Adjusts the weight according to the font name Pull Request resolved: https://github.com/facebook/react-native/pull/47742 Test Plan: Demo in https://github.com/facebook/react-native/issues/47656#issuecomment-2486282496. Reviewed By: cipolleschi Differential Revision: D66236128 Pulled By: javache fbshipit-source-id: d325a7fee28681a1e95fa0341cb7a16fcd9918c0 --- packages/react-native/React/Views/RCTFont.h | 2 ++ packages/react-native/React/Views/RCTFont.mm | 9 ++++----- .../react/renderer/textlayoutmanager/RCTFontUtils.mm | 11 ++++------- 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/packages/react-native/React/Views/RCTFont.h b/packages/react-native/React/Views/RCTFont.h index 5ddc34da35f..85291cda9d3 100644 --- a/packages/react-native/React/Views/RCTFont.h +++ b/packages/react-native/React/Views/RCTFont.h @@ -10,6 +10,7 @@ #import typedef UIFont * (^RCTFontHandler)(CGFloat fontSize, NSString *fontWeightDescription); +typedef CGFloat RCTFontWeight; /** * React Native will use the System font for rendering by default. If you want to @@ -19,6 +20,7 @@ typedef UIFont * (^RCTFontHandler)(CGFloat fontSize, NSString *fontWeightDescrip */ RCT_EXTERN void RCTSetDefaultFontHandler(RCTFontHandler handler); RCT_EXTERN BOOL RCTHasFontHandlerSet(void); +RCT_EXTERN RCTFontWeight RCTGetFontWeight(UIFont *font); @interface RCTFont : NSObject diff --git a/packages/react-native/React/Views/RCTFont.mm b/packages/react-native/React/Views/RCTFont.mm index a5a84bb2cb3..594ea127998 100644 --- a/packages/react-native/React/Views/RCTFont.mm +++ b/packages/react-native/React/Views/RCTFont.mm @@ -11,8 +11,7 @@ #import -typedef CGFloat RCTFontWeight; -static RCTFontWeight weightOfFont(UIFont *font) +RCTFontWeight RCTGetFontWeight(UIFont *font) { static NSArray *weightSuffixes; static NSArray *fontWeights; @@ -405,7 +404,7 @@ RCT_ARRAY_CONVERTER(RCTFontVariantDescriptor) if (font) { familyName = font.familyName ?: defaultFontFamily; fontSize = font.pointSize ?: defaultFontSize; - fontWeight = weightOfFont(font); + fontWeight = RCTGetFontWeight(font); isItalic = isItalicFont(font); isCondensed = isCondensedFont(font); } @@ -453,7 +452,7 @@ RCT_ARRAY_CONVERTER(RCTFontVariantDescriptor) // It's actually a font name, not a font family name, // but we'll do what was meant, not what was said. familyName = font.familyName; - fontWeight = weight ? fontWeight : weightOfFont(font); + fontWeight = weight ? fontWeight : RCTGetFontWeight(font); isItalic = style ? isItalic : isItalicFont(font); isCondensed = isCondensedFont(font); } else { @@ -476,7 +475,7 @@ RCT_ARRAY_CONVERTER(RCTFontVariantDescriptor) for (NSString *name in names) { UIFont *match = [UIFont fontWithName:name size:fontSize]; if (isItalic == isItalicFont(match) && isCondensed == isCondensedFont(match)) { - CGFloat testWeight = weightOfFont(match); + CGFloat testWeight = RCTGetFontWeight(match); if (ABS(testWeight - fontWeight) < ABS(closestWeight - fontWeight)) { font = match; closestWeight = testWeight; diff --git a/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTFontUtils.mm b/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTFontUtils.mm index 5f744790a94..b0867346b08 100644 --- a/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTFontUtils.mm +++ b/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTFontUtils.mm @@ -7,6 +7,7 @@ #import "RCTFontUtils.h" #import +#import #import #import @@ -45,12 +46,6 @@ static RCTFontProperties RCTResolveFontProperties( return fontProperties; } -static UIFontWeight RCTGetFontWeight(UIFont *font) -{ - NSDictionary *traits = [font.fontDescriptor objectForKey:UIFontDescriptorTraitsAttribute]; - return [traits[UIFontWeightTrait] doubleValue]; -} - static RCTFontStyle RCTGetFontStyle(UIFont *font) { NSDictionary *traits = [font.fontDescriptor objectForKey:UIFontDescriptorTraitsAttribute]; @@ -165,6 +160,7 @@ UIFont *RCTFontWithFontProperties(RCTFontProperties fontProperties) font = RCTDefaultFontWithFontProperties(fontProperties); } else { NSArray *fontNames = [UIFont fontNamesForFamilyName:fontProperties.family]; + UIFontWeight fontWeight = fontProperties.weight; if (fontNames.count == 0) { // Gracefully handle being given a font name rather than font family, for @@ -172,6 +168,7 @@ UIFont *RCTFontWithFontProperties(RCTFontProperties fontProperties) font = [UIFont fontWithName:fontProperties.family size:effectiveFontSize]; if (font) { fontNames = [UIFont fontNamesForFamilyName:font.familyName]; + fontWeight = fontWeight ?: RCTGetFontWeight(font); } else { // Failback to system font. font = [UIFont systemFontOfSize:effectiveFontSize weight:fontProperties.weight]; @@ -189,7 +186,7 @@ UIFont *RCTFontWithFontProperties(RCTFontProperties fontProperties) } CGFloat testWeight = RCTGetFontWeight(fontMatch); - if (ABS(testWeight - fontProperties.weight) < ABS(closestWeight - fontProperties.weight)) { + if (ABS(testWeight - fontWeight) < ABS(closestWeight - fontWeight)) { font = fontMatch; closestWeight = testWeight; }