Use font name for weight if no weight property available

Summary:
Some custom fonts don't return a UIFontWeightTrait that is representative of their actual weight. For example, "AktivGrotesk-Light" and "AktivGrotesk-Medium" both return UIFontWeightTrait 0. While this is of course an issue with the font files themselves, licensing issues can make it difficult to modify them directly. When using these fonts, specifying weights in JS has no effect. I propose that if no weight information is available, we inspect the name of the font for weighting.
Closes https://github.com/facebook/react-native/pull/2113

Reviewed By: svcscm

Differential Revision: D2783383

Pulled By: nicklockwood

fb-gh-sync-id: 2a9caf99b6af74b7013ecc85417322b56c2dea20
This commit is contained in:
Ryan Gomba
2015-12-22 06:48:29 -08:00
committed by facebook-github-bot-3
parent 7b62dc22ad
commit 4472bb54c9

View File

@@ -475,8 +475,34 @@ RCT_ENUM_CONVERTER(RCTFontStyle, (@{
static RCTFontWeight RCTWeightOfFont(UIFont *font)
{
static NSDictionary *nameToWeight;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
nameToWeight = @{
@"normal": @(UIFontWeightRegular),
@"bold": @(UIFontWeightBold),
@"ultralight": @(UIFontWeightUltraLight),
@"thin": @(UIFontWeightThin),
@"light": @(UIFontWeightLight),
@"regular": @(UIFontWeightRegular),
@"medium": @(UIFontWeightMedium),
@"semibold": @(UIFontWeightSemibold),
@"bold": @(UIFontWeightBold),
@"heavy": @(UIFontWeightHeavy),
@"black": @(UIFontWeightBlack),
};
});
NSDictionary *traits = [font.fontDescriptor objectForKey:UIFontDescriptorTraitsAttribute];
return [traits[UIFontWeightTrait] doubleValue];
RCTFontWeight weight = [traits[UIFontWeightTrait] doubleValue];
if (weight == 0.0) {
for (NSString *name in nameToWeight) {
if ([font.fontName.lowercaseString hasSuffix:name]) {
return [nameToWeight[name] doubleValue];
}
}
}
return weight;
}
static BOOL RCTFontIsItalic(UIFont *font)