Permalink
Browse files

fix fontWeight regression

Summary:
fix the regression I mentioned in #15162 (comment)

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 #15825

Differential Revision: D5782142

Pulled By: shergin

fbshipit-source-id: 5346b0cb263e535c0b445e7a2912c452573248a5
  • Loading branch information...
nihgwu authored and ide committed Sep 7, 2017
1 parent 29e5deb commit 0a319978ffdce459da1d3db1f3a14e1bf137b5e2
Showing with 32 additions and 19 deletions.
  1. +32 −19 React/Views/RCTFont.mm
View
@@ -36,33 +36,46 @@
typedef CGFloat RCTFontWeight;
static RCTFontWeight weightOfFont(UIFont *font)
{
static NSDictionary *nameToWeight;
static NSArray *fontNames;
static NSArray *fontWeights;
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),
};
// We use two arrays instead of one map because
// the order is important for suffix matching.
fontNames = @[
@"normal",
@"ultralight",
@"thin",
@"light",
@"regular",
@"medium",
@"semibold",
@"bold",
@"heavy",
@"black"
];
fontWeights = @[
@(UIFontWeightRegular),
@(UIFontWeightUltraLight),
@(UIFontWeightThin),
@(UIFontWeightLight),
@(UIFontWeightRegular),
@(UIFontWeightMedium),
@(UIFontWeightSemibold),
@(UIFontWeightBold),
@(UIFontWeightHeavy),
@(UIFontWeightBlack)
];
});
for (NSString *name in nameToWeight) {
if ([font.fontName.lowercaseString hasSuffix:name]) {
return [nameToWeight[name] doubleValue];
for (NSInteger i = 0; i < fontNames.count; i++) {
if ([font.fontName.lowercaseString hasSuffix:fontNames[i]]) {
return (RCTFontWeight)[fontWeights[i] doubleValue];
}
}
NSDictionary *traits = [font.fontDescriptor objectForKey:UIFontDescriptorTraitsAttribute];
RCTFontWeight weight = [traits[UIFontWeightTrait] doubleValue];
return weight;
return (RCTFontWeight)[traits[UIFontWeightTrait] doubleValue];
}
static BOOL isItalicFont(UIFont *font)

0 comments on commit 0a31997

Please sign in to comment.