Skip to content

Commit 96e4f04

Browse files
committed
[Text] Add a virtual fontName called "System".
Add a virtual fontName called "System" that defaults to whatever the current system font is. (@nicklockwood #1611) It may break UI of existing apps.
1 parent d36de67 commit 96e4f04

File tree

8 files changed

+30
-4
lines changed

8 files changed

+30
-4
lines changed
53.4 KB
Loading
48.3 KB
Loading
60.3 KB
Loading
49 KB
Loading
84.3 KB
Loading
55.2 KB
Loading

Examples/UIExplorer/UIExplorerUnitTests/RCTConvert_UIFontTests.m

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ - (void)DISABLED_testWeight // task #7118691
4141
- (void)testSize
4242
{
4343
{
44-
UIFont *expected = [UIFont fontWithName:@"HelveticaNeue" size:18.5];
44+
UIFont *expected =
45+
[UIFont fontWithName:[UIFont systemFontOfSize:18.5].fontName size:18.5];
4546
UIFont *result = [RCTConvert UIFont:@{@"fontSize": @18.5}];
4647
RCTAssertEqualFonts(expected, result);
4748
}
@@ -68,13 +69,33 @@ - (void)testFamily
6869

6970
- (void)testStyle
7071
{
72+
NSString *systemFontName = [UIFont systemFontOfSize:14].fontName;
7173
{
72-
UIFont *expected = [UIFont fontWithName:@"HelveticaNeue-Italic" size:14];
74+
UIFont *bestMatch = [UIFont fontWithName:systemFontName size: 14];
75+
CGFloat closestWeight = INFINITY;
76+
77+
for (NSString *name in [UIFont fontNamesForFamilyName:systemFontName]) {
78+
UIFont *match = [UIFont fontWithName:name size:14];
79+
NSDictionary *traits = [match.fontDescriptor objectForKey:UIFontDescriptorTraitsAttribute];
80+
UIFontDescriptorSymbolicTraits symbolicTraits =
81+
[traits[UIFontSymbolicTrait] unsignedIntValue];
82+
BOOL isItalic = (symbolicTraits & UIFontDescriptorTraitItalic) != 0;
83+
BOOL isCondensed = (symbolicTraits & UIFontDescriptorTraitCondensed) != 0;
84+
CGFloat weightOfFont = [traits[UIFontWeightTrait] doubleValue];
85+
86+
if (isItalic && !isCondensed) {
87+
if (ABS(weightOfFont) < ABS(closestWeight)) {
88+
bestMatch = match;
89+
closestWeight = weightOfFont;
90+
}
91+
}
92+
}
93+
UIFont *expected = [UIFont fontWithName:bestMatch.fontName size:14];
7394
UIFont *result = [RCTConvert UIFont:@{@"fontStyle": @"italic"}];
7495
RCTAssertEqualFonts(expected, result);
7596
}
7697
{
77-
UIFont *expected = [UIFont fontWithName:@"HelveticaNeue" size:14];
98+
UIFont *expected = [UIFont fontWithName:systemFontName size:14];
7899
UIFont *result = [RCTConvert UIFont:@{@"fontStyle": @"normal"}];
79100
RCTAssertEqualFonts(expected, result);
80101
}

React/Base/RCTConvert.m

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,7 @@ + (UIFont *)UIFont:(UIFont *)font withFamily:(id)family
746746
size:(id)size weight:(id)weight style:(id)style
747747
{
748748
// Defaults
749-
NSString *const RCTDefaultFontFamily = @"Helvetica Neue";
749+
NSString *const RCTDefaultFontFamily = @"System";
750750
const RCTFontWeight RCTDefaultFontWeight = UIFontWeightRegular;
751751
const CGFloat RCTDefaultFontSize = 14;
752752

@@ -757,6 +757,11 @@ + (UIFont *)UIFont:(UIFont *)font withFamily:(id)family
757757
BOOL isItalic = NO;
758758
BOOL isCondensed = NO;
759759

760+
if ([[self NSString:family] isEqualToString:RCTDefaultFontFamily] ||
761+
(!family && !font)) {
762+
font = [UIFont systemFontOfSize:fontSize];
763+
}
764+
760765
if (font) {
761766
familyName = font.familyName ?: RCTDefaultFontFamily;
762767
fontSize = font.pointSize ?: RCTDefaultFontSize;

0 commit comments

Comments
 (0)