Skip to content

Commit dff466e

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 dff466e

File tree

8 files changed

+29
-4
lines changed

8 files changed

+29
-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: 5 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

@@ -771,6 +771,10 @@ + (UIFont *)UIFont:(UIFont *)font withFamily:(id)family
771771
// Get font family
772772
familyName = [self NSString:family] ?: familyName;
773773

774+
if ([familyName isEqualToString: RCTDefaultFontFamily]) {
775+
familyName = [UIFont systemFontOfSize:fontSize].familyName;
776+
}
777+
774778
// Gracefully handle being given a font name rather than font family, for
775779
// example: "Helvetica Light Oblique" rather than just "Helvetica".
776780
if ([UIFont fontNamesForFamilyName:familyName].count == 0) {

0 commit comments

Comments
 (0)