From 64b30a937607e436b7979e245c456e7ca45bfd11 Mon Sep 17 00:00:00 2001 From: Pieter De Baets Date: Mon, 15 Sep 2025 11:31:30 -0700 Subject: [PATCH] Fix newarch ignoring defaultfonthandler (#53755) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/53755 Changelog: [iOS][Fixed] Make `RCTSetDefaultFontHandler` compatible with the new arch, and add a more powerful version as `RCTSetDefaultFontResolver` Reviewed By: fkgozali Differential Revision: D82207676 fbshipit-source-id: eeeaf708491de9156ef4f1e045864e4322213902 --- .../React/Views/RCTFont+Private.h | 16 ++++++++++ packages/react-native/React/Views/RCTFont.h | 4 ++- packages/react-native/React/Views/RCTFont.mm | 15 ++++++++-- .../renderer/textlayoutmanager/RCTFontUtils.h | 12 ++++++-- .../textlayoutmanager/RCTFontUtils.mm | 29 +++++++++++++++---- 5 files changed, 66 insertions(+), 10 deletions(-) create mode 100644 packages/react-native/React/Views/RCTFont+Private.h diff --git a/packages/react-native/React/Views/RCTFont+Private.h b/packages/react-native/React/Views/RCTFont+Private.h new file mode 100644 index 00000000000..4bfd7f66760 --- /dev/null +++ b/packages/react-native/React/Views/RCTFont+Private.h @@ -0,0 +1,16 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#import + +#import + +NS_ASSUME_NONNULL_BEGIN + +RCT_EXTERN UIFont *__nullable RCTGetLegacyDefaultFont(CGFloat fontSize, UIFontWeight fontWeight); + +NS_ASSUME_NONNULL_END diff --git a/packages/react-native/React/Views/RCTFont.h b/packages/react-native/React/Views/RCTFont.h index 85291cda9d3..6a31c85ee69 100644 --- a/packages/react-native/React/Views/RCTFont.h +++ b/packages/react-native/React/Views/RCTFont.h @@ -17,8 +17,10 @@ typedef CGFloat RCTFontWeight; * provide a different base font, use this override. The font weight supplied to your * handler will be one of "ultralight", "thin", "light", "regular", "medium", * "semibold", "extrabold", "bold", "heavy", or "black". + * + * @deprecated Use RCTSetDefaultFontResolver */ -RCT_EXTERN void RCTSetDefaultFontHandler(RCTFontHandler handler); +RCT_EXTERN void RCTSetDefaultFontHandler(RCTFontHandler handler) __attribute__((deprecated)); RCT_EXTERN BOOL RCTHasFontHandlerSet(void); RCT_EXTERN RCTFontWeight RCTGetFontWeight(UIFont *font); diff --git a/packages/react-native/React/Views/RCTFont.mm b/packages/react-native/React/Views/RCTFont.mm index 6fd79fff60a..675f40c3131 100644 --- a/packages/react-native/React/Views/RCTFont.mm +++ b/packages/react-native/React/Views/RCTFont.mm @@ -6,6 +6,8 @@ */ #import "RCTFont.h" +#import "RCTFont+Private.h" + #import "RCTAssert.h" #import "RCTLog.h" @@ -122,6 +124,15 @@ static NSString *FontWeightDescriptionFromUIFontWeight(UIFontWeight fontWeight) return @"regular"; } +UIFont *RCTGetLegacyDefaultFont(CGFloat size, UIFontWeight fontWeight) +{ + if (defaultFontHandler != nil) { + return defaultFontHandler(size, FontWeightDescriptionFromUIFontWeight(fontWeight)); + } else { + return nil; + } +} + static UIFont *cachedSystemFont(CGFloat size, RCTFontWeight weight) { static NSCache *fontCache = [NSCache new]; @@ -135,8 +146,8 @@ static UIFont *cachedSystemFont(CGFloat size, RCTFontWeight weight) NSValue *cacheKey = [[NSValue alloc] initWithBytes:&key objCType:@encode(CacheKey)]; UIFont *font = [fontCache objectForKey:cacheKey]; - if (!font) { - if (defaultFontHandler) { + if (font == nil) { + if (defaultFontHandler != nil) { NSString *fontWeightDescription = FontWeightDescriptionFromUIFontWeight(weight); font = defaultFontHandler(size, fontWeightDescription); } else { diff --git a/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTFontUtils.h b/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTFontUtils.h index ad4d7af3dd9..1558aa90d47 100644 --- a/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTFontUtils.h +++ b/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTFontUtils.h @@ -5,15 +5,23 @@ * LICENSE file in the root directory of this source tree. */ +#import #import - #import NS_ASSUME_NONNULL_BEGIN +using RCTDefaultFontResolver = UIFont *__nullable (^)(const RCTFontProperties &); + +/** + * React Native will use the System font for rendering by default. If you want to + * provide a different base font, use this override. + */ +RCT_EXTERN void RCTSetDefaultFontResolver(RCTDefaultFontResolver handler); + /** * Returns UIFont instance corresponded to given font properties. */ -UIFont *RCTFontWithFontProperties(RCTFontProperties fontProperties); +RCT_EXTERN UIFont *RCTFontWithFontProperties(RCTFontProperties fontProperties); NS_ASSUME_NONNULL_END 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 c158c099be0..9b04cadc22f 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 @@ -6,7 +6,9 @@ */ #import "RCTFontUtils.h" + #import +#import #import #import @@ -246,7 +248,14 @@ static NSArray *RCTFontFeatures(RCTFontVariant fontVariant) return fontFeatures; } -static UIFont *RCTDefaultFontWithFontProperties(RCTFontProperties fontProperties) +static RCTDefaultFontResolver defaultFontResolver; + +void RCTSetDefaultFontResolver(RCTDefaultFontResolver handler) +{ + defaultFontResolver = handler; +} + +static UIFont *RCTDefaultFontWithFontProperties(const RCTFontProperties &fontProperties) { static NSCache *fontCache; static std::mutex fontCacheMutex; @@ -261,14 +270,24 @@ static UIFont *RCTDefaultFontWithFontProperties(RCTFontProperties fontProperties { std::lock_guard lock(fontCacheMutex); - if (fontCache == nullptr) { + if (fontCache == nil) { fontCache = [NSCache new]; } font = [fontCache objectForKey:cacheKey]; } - if (font == nullptr) { - font = [UIFont systemFontOfSize:effectiveFontSize weight:fontProperties.weight]; + if (font == nil) { + if (defaultFontResolver != nil) { + font = defaultFontResolver(fontProperties); + } + + if (font == nil) { + font = RCTGetLegacyDefaultFont(effectiveFontSize, fontProperties.weight); + } + + if (font == nil) { + font = [UIFont systemFontOfSize:effectiveFontSize weight:fontProperties.weight]; + } BOOL isItalicFont = fontProperties.style == RCTFontStyleItalic; BOOL isCondensedFont = [fontProperties.family isEqualToString:@"SystemCondensed"]; @@ -366,7 +385,7 @@ UIFont *RCTFontWithFontProperties(RCTFontProperties fontProperties) } } - if (font == nullptr) { + if (font == nil) { // If we still don't have a match at least return the first font in the // fontFamily This is to support built-in font Zapfino and other custom // single font families like Impact