From 05c3755a583f92abcb68410bde9d46b24c40f735 Mon Sep 17 00:00:00 2001 From: Kip Ricker Date: Thu, 6 Jul 2017 22:56:44 -0700 Subject: [PATCH] iOS: Fix font weight resolution **Issue:** Some fonts are defined with weights that don't match with the UIFontWeight constants. **Example:** UIFontWeightTraits for Roboto font Light: -0.230 Thin: -0.365 Currently, the UIFontWeightTrait is always used if it != 0.0, and given the UIFontWeight constants for Light and Thin: UIFontWeightThin -0.6 UIFontWeightLight -0.4 A style font weight of "300" or "200" will both resolve to Roboto-Thin as its weight -0.365 is closer to -0.4 (UIFontWeightLight) and -0.6 (UIFontWeightThin) than -0.230 (Roboto-Light). **Proposed fix:** When resolving `getWeightOfFont` try to match the name of weight to the name of the font first, and guess the font with UIFontWeightTrait as the fall back. **Test Plan:** Attempt to display Roboto at weights "200" and "300" and Roboto-Thin and Roboto-Light should be displayed correctly. --- React/Views/RCTFont.mm | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/React/Views/RCTFont.mm b/React/Views/RCTFont.mm index 23286356548d99..98164b95780007 100644 --- a/React/Views/RCTFont.mm +++ b/React/Views/RCTFont.mm @@ -54,15 +54,14 @@ static RCTFontWeight weightOfFont(UIFont *font) }; }); - NSDictionary *traits = [font.fontDescriptor objectForKey:UIFontDescriptorTraitsAttribute]; - RCTFontWeight weight = [traits[UIFontWeightTrait] doubleValue]; - if (weight == 0.0) { - for (NSString *name in nameToWeight) { - if ([font.fontName.lowercaseString hasSuffix:name]) { - return [nameToWeight[name] doubleValue]; - } + for (NSString *name in nameToWeight) { + if ([font.fontName.lowercaseString hasSuffix:name]) { + return [nameToWeight[name] doubleValue]; } } + + NSDictionary *traits = [font.fontDescriptor objectForKey:UIFontDescriptorTraitsAttribute]; + RCTFontWeight weight = [traits[UIFontWeightTrait] doubleValue]; return weight; }