Summary:
fix the regression I mentioned in https://github.com/facebook/react-native/pull/15162#issuecomment-319696706

as no one is working on this, I take the step, although I know nothing about Objective C

I find the key point is that the keys in `NSDictionary` are not ordered as presented, it's a hash table, so no grantee on keys order, so I create a new array to do that, then it will check `ultralight` before `light` and `semibold` before `bold`
Closes https://github.com/facebook/react-native/pull/15825

Differential Revision: D5782142

Pulled By: shergin

fbshipit-source-id: 5346b0cb263e535c0b445e7a2912c452573248a5
This commit is contained in:
Neo 2017-09-06 23:43:17 -07:00 коммит произвёл Facebook Github Bot
Родитель 7b962397b6
Коммит d3007b0fd2
1 изменённых файлов: 32 добавлений и 19 удалений

Просмотреть файл

@ -36,33 +36,46 @@
typedef CGFloat RCTFontWeight; typedef CGFloat RCTFontWeight;
static RCTFontWeight weightOfFont(UIFont *font) static RCTFontWeight weightOfFont(UIFont *font)
{ {
static NSDictionary *nameToWeight; static NSArray *fontNames;
static NSArray *fontWeights;
static dispatch_once_t onceToken; static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{ dispatch_once(&onceToken, ^{
nameToWeight = @{ // We use two arrays instead of one map because
@"normal": @(UIFontWeightRegular), // the order is important for suffix matching.
@"bold": @(UIFontWeightBold), fontNames = @[
@"ultralight": @(UIFontWeightUltraLight), @"normal",
@"thin": @(UIFontWeightThin), @"ultralight",
@"light": @(UIFontWeightLight), @"thin",
@"regular": @(UIFontWeightRegular), @"light",
@"medium": @(UIFontWeightMedium), @"regular",
@"semibold": @(UIFontWeightSemibold), @"medium",
@"bold": @(UIFontWeightBold), @"semibold",
@"heavy": @(UIFontWeightHeavy), @"bold",
@"black": @(UIFontWeightBlack), @"heavy",
}; @"black"
];
fontWeights = @[
@(UIFontWeightRegular),
@(UIFontWeightUltraLight),
@(UIFontWeightThin),
@(UIFontWeightLight),
@(UIFontWeightRegular),
@(UIFontWeightMedium),
@(UIFontWeightSemibold),
@(UIFontWeightBold),
@(UIFontWeightHeavy),
@(UIFontWeightBlack)
];
}); });
for (NSString *name in nameToWeight) { for (NSInteger i = 0; i < fontNames.count; i++) {
if ([font.fontName.lowercaseString hasSuffix:name]) { if ([font.fontName.lowercaseString hasSuffix:fontNames[i]]) {
return [nameToWeight[name] doubleValue]; return (RCTFontWeight)[fontWeights[i] doubleValue];
} }
} }
NSDictionary *traits = [font.fontDescriptor objectForKey:UIFontDescriptorTraitsAttribute]; NSDictionary *traits = [font.fontDescriptor objectForKey:UIFontDescriptorTraitsAttribute];
RCTFontWeight weight = [traits[UIFontWeightTrait] doubleValue]; return (RCTFontWeight)[traits[UIFontWeightTrait] doubleValue];
return weight;
} }
static BOOL isItalicFont(UIFont *font) static BOOL isItalicFont(UIFont *font)