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
This commit is contained in:
zhongwuzw
2024-12-04 16:32:58 +00:00
committed by Blake Friedman
parent 9f1c6bcc13
commit bc04bb4072
3 changed files with 10 additions and 12 deletions
@@ -10,6 +10,7 @@
#import <React/RCTConvert.h>
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
+4 -5
View File
@@ -11,8 +11,7 @@
#import <CoreText/CoreText.h>
typedef CGFloat RCTFontWeight;
static RCTFontWeight weightOfFont(UIFont *font)
RCTFontWeight RCTGetFontWeight(UIFont *font)
{
static NSArray<NSString *> *weightSuffixes;
static NSArray<NSNumber *> *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;
@@ -7,6 +7,7 @@
#import "RCTFontUtils.h"
#import <CoreText/CoreText.h>
#import <React/RCTFont.h>
#import <algorithm>
#import <cmath>
@@ -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<NSString *> *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;
}