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
This commit is contained in:
Pieter De Baets authored and Facebook GitHub Bot committed 2025-09-15 11:31:30 -07:00
1 parent 07da2ff3e1
commit 64b30a9376
5 files changed
+66 -10

No files matched your search

@@ -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 <UIKit/UIKit.h>
#import <React/RCTDefines.h>
NS_ASSUME_NONNULL_BEGIN
RCT_EXTERN UIFont *__nullable RCTGetLegacyDefaultFont(CGFloat fontSize, UIFontWeight fontWeight);
NS_ASSUME_NONNULL_END
+3 -1
View File
@@ -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);
+13 -2
View File
@@ -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<NSValue *, UIFont *> *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 {
@@ -5,15 +5,23 @@
* LICENSE file in the root directory of this source tree.
*/
#import <React/RCTDefines.h>
#import <UIKit/UIKit.h>
#import <react/renderer/textlayoutmanager/RCTFontProperties.h>
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
@@ -6,7 +6,9 @@
*/
#import "RCTFontUtils.h"
#import <CoreText/CoreText.h>
#import <React/RCTFont+Private.h>
#import <React/RCTFont.h>
#import <algorithm>
@@ -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<std::mutex> 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