Skip to content

Commit e30327c

Browse files
javacheFacebook Github Bot 2
authored andcommitted
Fix styling of system fonts
Summary: When you call `-[RCTConvert UIFont:withFamily:...]` with a non-nil font object, we'll try to use the existing font object for system information. On iOS9+ which uses the San Francisco font, `[UIFont fontNamesForFamilyName:]` doesn't return anything useful, so we need to make sure that we detect this as a system font and use the appropriate methods. This issues is made worse by the fact that RCTTextView and friends recreate the font property for every attribute that is set (pretty horrible perf-wise). This fixes #2140 Reviewed By: sahrens Differential Revision: D3662751 fbshipit-source-id: c528e8945ed361a922c03f861d3c0b584658573b
1 parent 8f75d73 commit e30327c

File tree

1 file changed

+18
-37
lines changed

1 file changed

+18
-37
lines changed

React/Base/RCTConvert.m

Lines changed: 18 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -481,24 +481,6 @@ + (CGColorRef)CGColor:(id)json
481481
return [self UIColor:json].CGColor;
482482
}
483483

484-
#if !defined(__IPHONE_8_2) || __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_8_2
485-
486-
// These constants are defined in iPhone SDK 8.2, but the app cannot run on
487-
// iOS < 8.2 unless we redefine them here. If you target iOS 8.2 or above
488-
// as a base target, the standard constants will be used instead.
489-
490-
#define UIFontWeightUltraLight -0.8
491-
#define UIFontWeightThin -0.6
492-
#define UIFontWeightLight -0.4
493-
#define UIFontWeightRegular 0
494-
#define UIFontWeightMedium 0.23
495-
#define UIFontWeightSemibold 0.3
496-
#define UIFontWeightBold 0.4
497-
#define UIFontWeightHeavy 0.56
498-
#define UIFontWeightBlack 0.62
499-
500-
#endif
501-
502484
typedef CGFloat RCTFontWeight;
503485
RCT_ENUM_CONVERTER(RCTFontWeight, (@{
504486
@"normal": @(UIFontWeightRegular),
@@ -575,49 +557,52 @@ + (UIFont *)UIFont:(id)json
575557
size:json[@"fontSize"]
576558
weight:json[@"fontWeight"]
577559
style:json[@"fontStyle"]
578-
scaleMultiplier:1.0f];
560+
scaleMultiplier:1];
579561
}
580562

581563
+ (UIFont *)UIFont:(UIFont *)font withSize:(id)json
582564
{
583-
return [self UIFont:font withFamily:nil size:json weight:nil style:nil scaleMultiplier:1.0];
565+
return [self UIFont:font withFamily:nil size:json weight:nil style:nil scaleMultiplier:1];
584566
}
585567

586568
+ (UIFont *)UIFont:(UIFont *)font withWeight:(id)json
587569
{
588-
return [self UIFont:font withFamily:nil size:nil weight:json style:nil scaleMultiplier:1.0];
570+
return [self UIFont:font withFamily:nil size:nil weight:json style:nil scaleMultiplier:1];
589571
}
590572

591573
+ (UIFont *)UIFont:(UIFont *)font withStyle:(id)json
592574
{
593-
return [self UIFont:font withFamily:nil size:nil weight:nil style:json scaleMultiplier:1.0];
575+
return [self UIFont:font withFamily:nil size:nil weight:nil style:json scaleMultiplier:1];
594576
}
595577

596578
+ (UIFont *)UIFont:(UIFont *)font withFamily:(id)json
597579
{
598-
return [self UIFont:font withFamily:json size:nil weight:nil style:nil scaleMultiplier:1.0];
580+
return [self UIFont:font withFamily:json size:nil weight:nil style:nil scaleMultiplier:1];
599581
}
600582

601583
+ (UIFont *)UIFont:(UIFont *)font withFamily:(id)family
602584
size:(id)size weight:(id)weight style:(id)style
603585
scaleMultiplier:(CGFloat)scaleMultiplier
604586
{
605587
// Defaults
606-
NSString *const RCTDefaultFontFamily = @"System";
607-
NSString *const RCTIOS8SystemFontFamily = @"Helvetica Neue";
608-
const RCTFontWeight RCTDefaultFontWeight = UIFontWeightRegular;
609-
const CGFloat RCTDefaultFontSize = 14;
588+
static NSString *defaultFontFamily;
589+
static dispatch_once_t onceToken;
590+
dispatch_once(&onceToken, ^{
591+
defaultFontFamily = [UIFont systemFontOfSize:14].familyName;
592+
});
593+
const RCTFontWeight defaultFontWeight = UIFontWeightRegular;
594+
const CGFloat defaultFontSize = 14;
610595

611596
// Initialize properties to defaults
612-
CGFloat fontSize = RCTDefaultFontSize;
613-
RCTFontWeight fontWeight = RCTDefaultFontWeight;
614-
NSString *familyName = RCTDefaultFontFamily;
597+
CGFloat fontSize = defaultFontSize;
598+
RCTFontWeight fontWeight = defaultFontWeight;
599+
NSString *familyName = defaultFontFamily;
615600
BOOL isItalic = NO;
616601
BOOL isCondensed = NO;
617602

618603
if (font) {
619-
familyName = font.familyName ?: RCTDefaultFontFamily;
620-
fontSize = font.pointSize ?: RCTDefaultFontSize;
604+
familyName = font.familyName ?: defaultFontFamily;
605+
fontSize = font.pointSize ?: defaultFontSize;
621606
fontWeight = RCTWeightOfFont(font);
622607
isItalic = RCTFontIsItalic(font);
623608
isCondensed = RCTFontIsCondensed(font);
@@ -634,7 +619,7 @@ + (UIFont *)UIFont:(UIFont *)font withFamily:(id)family
634619

635620
// Handle system font as special case. This ensures that we preserve
636621
// the specific metrics of the standard system font as closely as possible.
637-
if ([familyName isEqual:RCTDefaultFontFamily]) {
622+
if ([familyName isEqual:defaultFontFamily] || [familyName isEqualToString:@"System"]) {
638623
if ([UIFont respondsToSelector:@selector(systemFontOfSize:weight:)]) {
639624
font = [UIFont systemFontOfSize:fontSize weight:fontWeight];
640625
if (isItalic || isCondensed) {
@@ -650,10 +635,6 @@ + (UIFont *)UIFont:(UIFont *)font withFamily:(id)family
650635
font = [UIFont fontWithDescriptor:fontDescriptor size:fontSize];
651636
}
652637
return font;
653-
} else {
654-
// systemFontOfSize:weight: isn't available prior to iOS 8.2, so we
655-
// fall back to finding the correct font manually, by linear search.
656-
familyName = RCTIOS8SystemFontFamily;
657638
}
658639
}
659640

0 commit comments

Comments
 (0)