Skip to content

Commit fc05985

Browse files
committed
[Text] Get the system font instead of Helvetica programmatically and add a virtual fontName called "System"
Summary: Get the system font instead of Helvetica programmatically and add a virtual fontName called "System" that defaults to whatever the current system font is. #1611 Closes #1635 Github Author: LYK <[email protected]>
1 parent 10bb054 commit fc05985

File tree

11 files changed

+87
-48
lines changed

11 files changed

+87
-48
lines changed
459 Bytes
Loading
10 Bytes
Loading
-526 Bytes
Loading
-85 Bytes
Loading
787 Bytes
Loading
1.78 KB
Loading

Examples/UIExplorer/UIExplorerUnitTests/RCTConvert_UIFontTests.m

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,25 @@ @implementation RCTConvert_UIFontTests
1414
XCTAssertEqualObjects(font1, font2); \
1515
}
1616

17-
- (void)DISABLED_testWeight // task #7118691
17+
- (void)testWeight
1818
{
1919
{
20-
UIFont *expected = [UIFont fontWithName:@"HelveticaNeue-Bold" size:14];
20+
UIFont *expected = [UIFont systemFontOfSize:14 weight:UIFontWeightBold];
2121
UIFont *result = [RCTConvert UIFont:@{@"fontWeight": @"bold"}];
2222
RCTAssertEqualFonts(expected, result);
2323
}
2424
{
25-
UIFont *expected = [UIFont fontWithName:@"HelveticaNeue-Medium" size:14];
25+
UIFont *expected = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
2626
UIFont *result = [RCTConvert UIFont:@{@"fontWeight": @"500"}];
2727
RCTAssertEqualFonts(expected, result);
2828
}
2929
{
30-
UIFont *expected = [UIFont fontWithName:@"HelveticaNeue-UltraLight" size:14];
30+
UIFont *expected = [UIFont systemFontOfSize:14 weight:UIFontWeightUltraLight];
3131
UIFont *result = [RCTConvert UIFont:@{@"fontWeight": @"100"}];
3232
RCTAssertEqualFonts(expected, result);
3333
}
3434
{
35-
UIFont *expected = [UIFont fontWithName:@"HelveticaNeue" size:14];
35+
UIFont *expected = [UIFont systemFontOfSize:14 weight:UIFontWeightRegular];
3636
UIFont *result = [RCTConvert UIFont:@{@"fontWeight": @"normal"}];
3737
RCTAssertEqualFonts(expected, result);
3838
}
@@ -41,7 +41,7 @@ - (void)DISABLED_testWeight // task #7118691
4141
- (void)testSize
4242
{
4343
{
44-
UIFont *expected = [UIFont fontWithName:@"HelveticaNeue" size:18.5];
44+
UIFont *expected = [UIFont systemFontOfSize:18.5];
4545
UIFont *result = [RCTConvert UIFont:@{@"fontSize": @18.5}];
4646
RCTAssertEqualFonts(expected, result);
4747
}
@@ -69,32 +69,47 @@ - (void)testFamily
6969
- (void)testStyle
7070
{
7171
{
72-
UIFont *expected = [UIFont fontWithName:@"HelveticaNeue-Italic" size:14];
72+
UIFont *font = [UIFont systemFontOfSize:14];
73+
UIFontDescriptor *fontDescriptor = [font fontDescriptor];
74+
UIFontDescriptorSymbolicTraits symbolicTraits = fontDescriptor.symbolicTraits;
75+
symbolicTraits |= UIFontDescriptorTraitItalic;
76+
fontDescriptor = [fontDescriptor fontDescriptorWithSymbolicTraits:symbolicTraits];
77+
UIFont *expected = [UIFont fontWithDescriptor:fontDescriptor size:14];
7378
UIFont *result = [RCTConvert UIFont:@{@"fontStyle": @"italic"}];
7479
RCTAssertEqualFonts(expected, result);
7580
}
7681
{
77-
UIFont *expected = [UIFont fontWithName:@"HelveticaNeue" size:14];
82+
UIFont *expected = [UIFont systemFontOfSize:14];
7883
UIFont *result = [RCTConvert UIFont:@{@"fontStyle": @"normal"}];
7984
RCTAssertEqualFonts(expected, result);
8085
}
8186
}
8287

83-
- (void)DISABLED_testStyleAndWeight // task #7118691
88+
- (void)testStyleAndWeight
8489
{
8590
{
86-
UIFont *expected = [UIFont fontWithName:@"HelveticaNeue-UltraLightItalic" size:14];
91+
UIFont *font = [UIFont systemFontOfSize:14 weight:UIFontWeightUltraLight];
92+
UIFontDescriptor *fontDescriptor = [font fontDescriptor];
93+
UIFontDescriptorSymbolicTraits symbolicTraits = fontDescriptor.symbolicTraits;
94+
symbolicTraits |= UIFontDescriptorTraitItalic;
95+
fontDescriptor = [fontDescriptor fontDescriptorWithSymbolicTraits:symbolicTraits];
96+
UIFont *expected = [UIFont fontWithDescriptor:fontDescriptor size:14];
8797
UIFont *result = [RCTConvert UIFont:@{@"fontStyle": @"italic", @"fontWeight": @"100"}];
8898
RCTAssertEqualFonts(expected, result);
8999
}
90100
{
91-
UIFont *expected = [UIFont fontWithName:@"HelveticaNeue-BoldItalic" size:14];
101+
UIFont *font = [UIFont systemFontOfSize:14 weight:UIFontWeightBold];
102+
UIFontDescriptor *fontDescriptor = [font fontDescriptor];
103+
UIFontDescriptorSymbolicTraits symbolicTraits = fontDescriptor.symbolicTraits;
104+
symbolicTraits |= UIFontDescriptorTraitItalic;
105+
fontDescriptor = [fontDescriptor fontDescriptorWithSymbolicTraits:symbolicTraits];
106+
UIFont *expected = [UIFont fontWithDescriptor:fontDescriptor size:14];
92107
UIFont *result = [RCTConvert UIFont:@{@"fontStyle": @"italic", @"fontWeight": @"bold"}];
93108
RCTAssertEqualFonts(expected, result);
94109
}
95110
}
96111

97-
- (void)DISABLED_testFamilyAndWeight // task #7118691
112+
- (void)testFamilyAndWeight
98113
{
99114
{
100115
UIFont *expected = [UIFont fontWithName:@"HelveticaNeue-Bold" size:14];
@@ -111,11 +126,6 @@ - (void)DISABLED_testFamilyAndWeight // task #7118691
111126
UIFont *result = [RCTConvert UIFont:@{@"fontFamily": @"Cochin", @"fontWeight": @"700"}];
112127
RCTAssertEqualFonts(expected, result);
113128
}
114-
{
115-
UIFont *expected = [UIFont fontWithName:@"Cochin" size:14];
116-
UIFont *result = [RCTConvert UIFont:@{@"fontFamily": @"Cochin", @"fontWeight": @"500"}]; // regular Cochin is actually medium bold
117-
RCTAssertEqualFonts(expected, result);
118-
}
119129
{
120130
UIFont *expected = [UIFont fontWithName:@"Cochin" size:14];
121131
UIFont *result = [RCTConvert UIFont:@{@"fontFamily": @"Cochin", @"fontWeight": @"100"}];
@@ -137,7 +147,7 @@ - (void)testFamilyAndStyle
137147
}
138148
}
139149

140-
- (void)DISABLED_testFamilyStyleAndWeight // task #7118691
150+
- (void)testFamilyStyleAndWeight
141151
{
142152
{
143153
UIFont *expected = [UIFont fontWithName:@"HelveticaNeue-UltraLightItalic" size:14];
@@ -156,4 +166,18 @@ - (void)DISABLED_testFamilyStyleAndWeight // task #7118691
156166
}
157167
}
158168

169+
- (void)testInvalidFont
170+
{
171+
{
172+
UIFont *expected = [UIFont systemFontOfSize:14];
173+
UIFont *result = [RCTConvert UIFont:@{@"fontFamily": @"foobar"}];
174+
RCTAssertEqualFonts(expected, result);
175+
}
176+
{
177+
UIFont *expected = [UIFont boldSystemFontOfSize:14];
178+
UIFont *result = [RCTConvert UIFont:@{@"fontFamily": @"foobar", @"fontWeight": @"bold"}];
179+
RCTAssertEqualFonts(expected, result);
180+
}
181+
}
182+
159183
@end

Examples/UIExplorer/UIExplorerUnitTests/RCTUIManagerTests.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ - (void)testModifyIndices7
382382
XCTAssertEqualObjects(removeIndices, (@[@0, @1, @3, @4]));
383383
}
384384

385-
- (void)testScenario1
385+
- (void)DISABLED_testScenario1 // t7660646
386386
{
387387
RCTUIManager *uiManager = [[RCTUIManager alloc] init];
388388
RCTBridge *bridge = [[RCTBridge alloc] initWithBundleURL:nil moduleProvider:^{ return @[uiManager]; } launchOptions:nil];
@@ -454,7 +454,7 @@ - (void)testScenario1
454454
[self waitForExpectationsWithTimeout:1 handler:nil];
455455
}
456456

457-
- (void)testScenario2
457+
- (void)DISABLED_testScenario2 // t7660646
458458
{
459459
RCTUIManager *uiManager = [[RCTUIManager alloc] init];
460460
RCTBridge *bridge = [[RCTBridge alloc] initWithBundleURL:nil moduleProvider:^{ return @[uiManager]; } launchOptions:nil];

Libraries/Text/RCTTextView.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ - (BOOL)canBecomeFirstResponder
222222

223223
- (UIFont *)defaultPlaceholderFont
224224
{
225-
return [UIFont fontWithName:@"Helvetica" size:17];
225+
return [UIFont systemFontOfSize:17];
226226
}
227227

228228
- (UIColor *)defaultPlaceholderTextColor

React/Base/RCTConvert.m

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,8 @@ + (UIFont *)UIFont:(UIFont *)font withFamily:(id)family
788788
size:(id)size weight:(id)weight style:(id)style
789789
{
790790
// Defaults
791-
NSString *const RCTDefaultFontFamily = @"Helvetica Neue";
791+
NSString *const RCTDefaultFontFamily = @"System";
792+
NSString *const RCTIOS8SystemFontFamily = @"Helvetica Neue";
792793
const RCTFontWeight RCTDefaultFontWeight = UIFontWeightRegular;
793794
const CGFloat RCTDefaultFontSize = 14;
794795

@@ -807,11 +808,36 @@ + (UIFont *)UIFont:(UIFont *)font withFamily:(id)family
807808
isCondensed = RCTFontIsCondensed(font);
808809
}
809810

810-
// Get font size
811+
// Get font attributes
811812
fontSize = [self CGFloat:size] ?: fontSize;
812-
813-
// Get font family
814813
familyName = [self NSString:family] ?: familyName;
814+
isItalic = style ? [self RCTFontStyle:style] : isItalic;
815+
fontWeight = weight ? [self RCTFontWeight:weight] : fontWeight;
816+
817+
// Handle system font as special case. This ensures that we preserve
818+
// the specific metrics of the standard system font as closely as possible.
819+
if ([familyName isEqual:RCTDefaultFontFamily]) {
820+
if ([UIFont respondsToSelector:@selector(systemFontOfSize:weight:)]) {
821+
font = [UIFont systemFontOfSize:fontSize weight:fontWeight];
822+
if (isItalic || isCondensed) {
823+
UIFontDescriptor *fontDescriptor = [font fontDescriptor];
824+
UIFontDescriptorSymbolicTraits symbolicTraits = fontDescriptor.symbolicTraits;
825+
if (isItalic) {
826+
symbolicTraits |= UIFontDescriptorTraitItalic;
827+
}
828+
if (isCondensed) {
829+
symbolicTraits |= UIFontDescriptorTraitCondensed;
830+
}
831+
fontDescriptor = [fontDescriptor fontDescriptorWithSymbolicTraits:symbolicTraits];
832+
font = [UIFont fontWithDescriptor:fontDescriptor size:fontSize];
833+
}
834+
return font;
835+
} else {
836+
// systemFontOfSize:weight: isn't available prior to iOS 8.2, so we
837+
// fall back to finding the correct font manually, by linear search.
838+
familyName = RCTIOS8SystemFontFamily;
839+
}
840+
}
815841

816842
// Gracefully handle being given a font name rather than font family, for
817843
// example: "Helvetica Light Oblique" rather than just "Helvetica".
@@ -821,30 +847,25 @@ + (UIFont *)UIFont:(UIFont *)font withFamily:(id)family
821847
// It's actually a font name, not a font family name,
822848
// but we'll do what was meant, not what was said.
823849
familyName = font.familyName;
824-
fontWeight = RCTWeightOfFont(font);
825-
isItalic = RCTFontIsItalic(font);
850+
fontWeight = weight ? fontWeight : RCTWeightOfFont(font);
851+
isItalic = style ? isItalic : RCTFontIsItalic(font);
826852
isCondensed = RCTFontIsCondensed(font);
827853
} else {
828854
// Not a valid font or family
829855
RCTLogError(@"Unrecognized font family '%@'", familyName);
830-
familyName = RCTDefaultFontFamily;
856+
if ([UIFont respondsToSelector:@selector(systemFontOfSize:weight:)]) {
857+
font = [UIFont systemFontOfSize:fontSize weight:fontWeight];
858+
} else if (fontWeight > UIFontWeightRegular) {
859+
font = [UIFont boldSystemFontOfSize:fontSize];
860+
} else {
861+
font = [UIFont systemFontOfSize:fontSize];
862+
}
831863
}
832864
}
833865

834-
// Get font style
835-
if (style) {
836-
isItalic = [self RCTFontStyle:style];
837-
}
838-
839-
// Get font weight
840-
if (weight) {
841-
fontWeight = [self RCTFontWeight:weight];
842-
}
843-
844866
// Get the closest font that matches the given weight for the fontFamily
845-
UIFont *bestMatch = [UIFont fontWithName:font.fontName size: fontSize];
867+
UIFont *bestMatch = font;
846868
CGFloat closestWeight = INFINITY;
847-
848869
for (NSString *name in [UIFont fontNamesForFamilyName:familyName]) {
849870
UIFont *match = [UIFont fontWithName:name size:fontSize];
850871
if (isItalic == RCTFontIsItalic(match) &&
@@ -857,14 +878,6 @@ + (UIFont *)UIFont:(UIFont *)font withFamily:(id)family
857878
}
858879
}
859880

860-
// Safety net
861-
if (!bestMatch) {
862-
RCTLogError(@"Could not find font with family: '%@', size: %@, \
863-
weight: %@, style: %@", family, size, weight, style);
864-
bestMatch = [UIFont fontWithName:[[UIFont fontNamesForFamilyName:familyName] firstObject]
865-
size:fontSize];
866-
}
867-
868881
return bestMatch;
869882
}
870883

0 commit comments

Comments
 (0)