Permalink
Browse files

iOS: Fix font weight resolution

Summary:
**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.

Current:
![simulator screen shot jul 7 2017 11 44 42 am](https://user-images.githubusercontent.com/889895/28506859-31b274e8-6fe3-11e7-8f92-f41ff2183356.png)

Fixed:
![simulator screen shot jul 7 2017 11 42 25 am](https://user-images.githubusercontent.com/889895/28506861-365ea3f4-6fe3-11e7-992c-9f426785037f.png)
Closes #15162

Differential Revision: D5479817

Pulled By: javache

fbshipit-source-id: a9f93d8ce69a96fb685cb09393d1db42486cc0c2
  • Loading branch information...
Kip Ricker authored and facebook-github-bot committed Jul 24, 2017
1 parent 3683ff8 commit cfeaefb4e0e5070d9f500e32de77f5f1ec23fb7c
Showing with 6 additions and 7 deletions.
  1. +6 −7 React/Views/RCTFont.mm
View
@@ -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;
}

4 comments on commit cfeaefb

@henrikra

This comment has been minimized.

Show comment
Hide comment
@henrikra

henrikra replied Jul 25, 2017

Good fix!

@fungilation

This comment has been minimized.

Show comment
Hide comment
@fungilation

fungilation Sep 6, 2017

I had to fix/tweak new weights to put my app's look back to how it was before RN 0.48. How rendered font weight may change on RN 0.48 upgrade should be a bigger warning in release notes.

fungilation replied Sep 6, 2017

I had to fix/tweak new weights to put my app's look back to how it was before RN 0.48. How rendered font weight may change on RN 0.48 upgrade should be a bigger warning in release notes.

@wildseansy

This comment has been minimized.

Show comment
Hide comment
@wildseansy

wildseansy Sep 7, 2017

Contributor

@fungilation - was a breaking change for me too. Looks like application of bold vs. semibold, light vs. extralight is kind of broken in 48 too, but has now been fixed in this commit d3007b0

Contributor

wildseansy replied Sep 7, 2017

@fungilation - was a breaking change for me too. Looks like application of bold vs. semibold, light vs. extralight is kind of broken in 48 too, but has now been fixed in this commit d3007b0

@fungilation

This comment has been minimized.

Show comment
Hide comment
@fungilation

fungilation Sep 7, 2017

Interesting and nice fix

fungilation replied Sep 7, 2017

Interesting and nice fix

Please sign in to comment.