Skip to content

Commit 53bcc3d

Browse files
committed
[Text] RCTConvert uses fontDescriptor of System font or given fonts.
* System font + weight -> It uses fontDescriptor to generate font. * Other font -> uses fontDescriptor to getnere font. * Other font + weight/fontName -> It uses familyName to generate font. It looks like iOS doesn't support change weight dynamically.
1 parent 96e4f04 commit 53bcc3d

File tree

8 files changed

+55
-25
lines changed

8 files changed

+55
-25
lines changed
636 Bytes
Loading
-193 Bytes
Loading
-1.27 KB
Loading
218 Bytes
Loading
-2.25 KB
Loading
1.27 KB
Loading

Examples/UIExplorer/UIExplorerUnitTests/RCTConvert_UIFontTests.m

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

7069
- (void)testStyle
7170
{
72-
NSString *systemFontName = [UIFont systemFontOfSize:14].fontName;
7371
{
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];
72+
UIFont *font = [UIFont systemFontOfSize:14];
73+
UIFontDescriptor *fontDescriptor = [font fontDescriptor];
74+
UIFontDescriptorSymbolicTraits symbolicTraits =
75+
fontDescriptor.symbolicTraits;
76+
symbolicTraits |= UIFontDescriptorTraitItalic;
77+
fontDescriptor =
78+
[fontDescriptor fontDescriptorWithSymbolicTraits:symbolicTraits];
79+
UIFont *expected = [UIFont fontWithDescriptor:fontDescriptor size:14];
9480
UIFont *result = [RCTConvert UIFont:@{@"fontStyle": @"italic"}];
9581
RCTAssertEqualFonts(expected, result);
9682
}
9783
{
98-
UIFont *expected = [UIFont fontWithName:systemFontName size:14];
84+
UIFont *expected = [UIFont systemFontOfSize:14];
9985
UIFont *result = [RCTConvert UIFont:@{@"fontStyle": @"normal"}];
10086
RCTAssertEqualFonts(expected, result);
10187
}

React/Base/RCTConvert.m

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -759,10 +759,54 @@ + (UIFont *)UIFont:(UIFont *)font withFamily:(id)family
759759

760760
if ([[self NSString:family] isEqualToString:RCTDefaultFontFamily] ||
761761
(!family && !font)) {
762-
font = [UIFont systemFontOfSize:fontSize];
762+
if (font) {
763+
fontSize = font.pointSize ?: RCTDefaultFontSize;
764+
fontWeight = RCTWeightOfFont(font);
765+
isItalic = RCTFontIsItalic(font);
766+
}
767+
fontSize = [self CGFloat:size] ?: fontSize;
768+
fontWeight = [self RCTFontWeight:weight] ?: fontWeight;
769+
770+
if (weight) {
771+
font = [UIFont systemFontOfSize:fontSize weight:fontWeight];
772+
} else {
773+
font = [UIFont systemFontOfSize:fontSize];
774+
}
775+
776+
UIFontDescriptor *fontDescriptor = [font fontDescriptor];
777+
UIFontDescriptorSymbolicTraits symbolicTraits =
778+
fontDescriptor.symbolicTraits;
779+
if (style) {
780+
isItalic = [self RCTFontStyle:style];
781+
}
782+
if (isItalic) {
783+
symbolicTraits |= UIFontDescriptorTraitItalic;
784+
fontDescriptor =
785+
[fontDescriptor fontDescriptorWithSymbolicTraits:symbolicTraits];
786+
}
787+
return [UIFont fontWithDescriptor:fontDescriptor size:fontSize];
763788
}
764789

765790
if (font) {
791+
if (!family && !weight) {
792+
fontSize = font.pointSize ?: RCTDefaultFontSize;
793+
fontSize = [self CGFloat:size] ?: fontSize;
794+
795+
UIFontDescriptor *fontDescriptor = [font fontDescriptor];
796+
UIFontDescriptorSymbolicTraits symbolicTraits =
797+
fontDescriptor.symbolicTraits;
798+
799+
if (style) {
800+
isItalic = [self RCTFontStyle:style];
801+
}
802+
if (italic) {
803+
symbolicTraits |= UIFontDescriptorTraitItalic;
804+
fontDescriptor =
805+
[fontDescriptor fontDescriptorWithSymbolicTraits:symbolicTraits];
806+
}
807+
return [UIFont fontWithDescriptor:fontDescriptor size:fontSize];
808+
}
809+
766810
familyName = font.familyName ?: RCTDefaultFontFamily;
767811
fontSize = font.pointSize ?: RCTDefaultFontSize;
768812
fontWeight = RCTWeightOfFont(font);

0 commit comments

Comments
 (0)