Skip to content

Commit 07da2ff

Browse files
ismarbesicfacebook-github-bot
authored andcommitted
feat(ios): support condensed system font on fabric (#52259)
Summary: This PR adds support for using the condensed system font on iOS when passing "SystemCondensed" as fontFamily. This behavior existed in the old architecture but was never ported to the new one, see [RCTFont.mm](https://github.com/facebook/react-native/blob/main/packages/react-native/React/Views/RCTFont.mm#L434) as reference. Fixes #52258. ## Changelog: [IOS] [ADDED] - Add support for condensed system font when using the new react native architecture. Pull Request resolved: #52259 Test Plan: Before: <img width="275" src="https://github.com/user-attachments/assets/8744a5ae-252c-46db-b5f9-b803f3e1c671" /> After: <img width="275" src="https://github.com/user-attachments/assets/69ec27a3-5c9a-46e3-a80a-0e02b76d8813" /> Reviewed By: cortinico Differential Revision: D82208140 Pulled By: javache fbshipit-source-id: b23a97c94bf45144c3f0860c30e35cae88c7dc2f
1 parent 90ac3ac commit 07da2ff

File tree

2 files changed

+30
-36
lines changed
  • packages

2 files changed

+30
-36
lines changed

packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTFontUtils.mm

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,11 @@ static RCTFontStyle RCTGetFontStyle(UIFont *font)
252252
static std::mutex fontCacheMutex;
253253

254254
CGFloat effectiveFontSize = fontProperties.sizeMultiplier * fontProperties.size;
255-
NSString *cacheKey = [NSString
256-
stringWithFormat:@"%.1f/%.2f/%ld", effectiveFontSize, fontProperties.weight, (long)fontProperties.style];
255+
NSString *cacheKey = [NSString stringWithFormat:@"%@/%.1f/%.2f/%ld",
256+
fontProperties.family,
257+
effectiveFontSize,
258+
fontProperties.weight,
259+
(long)fontProperties.style];
257260
UIFont *font;
258261

259262
{
@@ -267,11 +270,20 @@ static RCTFontStyle RCTGetFontStyle(UIFont *font)
267270
if (font == nullptr) {
268271
font = [UIFont systemFontOfSize:effectiveFontSize weight:fontProperties.weight];
269272

270-
if (fontProperties.style == RCTFontStyleItalic) {
273+
BOOL isItalicFont = fontProperties.style == RCTFontStyleItalic;
274+
BOOL isCondensedFont = [fontProperties.family isEqualToString:@"SystemCondensed"];
275+
276+
if (isItalicFont || isCondensedFont) {
271277
UIFontDescriptor *fontDescriptor = [font fontDescriptor];
272278
UIFontDescriptorSymbolicTraits symbolicTraits = fontDescriptor.symbolicTraits;
273279

274-
symbolicTraits |= UIFontDescriptorTraitItalic;
280+
if (isItalicFont) {
281+
symbolicTraits |= UIFontDescriptorTraitItalic;
282+
}
283+
284+
if (isCondensedFont) {
285+
symbolicTraits |= UIFontDescriptorTraitCondensed;
286+
}
275287

276288
fontDescriptor = [fontDescriptor fontDescriptorWithSymbolicTraits:symbolicTraits];
277289
font = [UIFont fontWithDescriptor:fontDescriptor size:effectiveFontSize];
@@ -333,7 +345,7 @@ static UIFontDescriptorSystemDesign RCTGetFontDescriptorSystemDesign(NSString *f
333345
fontWeight = (fontWeight != 0.0) ?: RCTGetFontWeight(font);
334346
} else {
335347
// Failback to system font.
336-
font = [UIFont systemFontOfSize:effectiveFontSize weight:fontProperties.weight];
348+
font = RCTDefaultFontWithFontProperties(fontProperties);
337349
}
338350
}
339351

packages/rn-tester/js/examples/Text/TextExample.ios.js

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -577,42 +577,23 @@ const examples = [
577577
description:
578578
('Shows system font families including system-ui/ui-sans-serif, ui-serif, ui-monospace, and ui-rounded': string),
579579
render: function (): React.Node {
580+
const baseTextStyle = {fontSize: 20};
580581
return (
581-
<View testID={'ios-font-families'}>
582-
<Text
583-
style={{
584-
fontFamily: 'system-ui',
585-
fontSize: 32,
586-
marginBottom: 20,
587-
}}>
588-
`fontFamily: system-ui` (same as `ui-sans-serif`)
582+
<View testID={'ios-font-families'} style={{gap: 10}}>
583+
<Text style={{...baseTextStyle, fontFamily: 'system-ui'}}>
584+
system-ui (same as ui-sans-serif)
589585
</Text>
590-
<Text
591-
style={{
592-
fontFamily: 'ui-sans-serif',
593-
fontSize: 32,
594-
marginBottom: 20,
595-
}}>
596-
`fontFamily: ui-sans-serif` (same as `system-ui`)
586+
<Text style={{...baseTextStyle, fontFamily: 'ui-sans-serif'}}>
587+
ui-sans-serif (same as system-ui)
597588
</Text>
598-
<Text
599-
style={{fontFamily: 'ui-serif', fontSize: 32, marginBottom: 20}}>
600-
`fontFamily: ui-serif`
589+
<Text style={{...baseTextStyle, fontFamily: 'ui-serif'}}>
590+
ui-serif
601591
</Text>
602-
<Text
603-
style={{
604-
fontFamily: 'ui-monospace',
605-
fontSize: 32,
606-
marginBottom: 20,
607-
}}>
608-
`fontFamily: ui-monospace`
592+
<Text style={{...baseTextStyle, fontFamily: 'ui-monospace'}}>
593+
ui-monospace
609594
</Text>
610-
<Text
611-
style={{
612-
fontFamily: 'ui-rounded',
613-
fontSize: 32,
614-
}}>
615-
`fontFamily: ui-rounded`
595+
<Text style={{...baseTextStyle, fontFamily: 'ui-rounded'}}>
596+
ui-rounded
616597
</Text>
617598
</View>
618599
);
@@ -767,6 +748,7 @@ const examples = [
767748
}}>
768749
Verdana bold
769750
</Text>
751+
<Text style={{fontFamily: 'SystemCondensed'}}>SystemCondensed</Text>
770752
<Text style={{fontFamily: 'Unknown Font Family'}}>
771753
Unknown Font Family
772754
</Text>

0 commit comments

Comments
 (0)