Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
iOS: Support allowFontScaling on TextInput #14030
As part of this change, the TextInput setters for font attributes (e.g. size, weight) had to be refactored. The problem with them is that they use RCTFont's helpers which create a new font based on an existing font. These helpers lose information. In particular, they lose the scaleMultiplier.
For example, suppose the font size is 12 and the device's font multiplier is set to 1.5. So we'd create a font with size 12 and scaleMultiplier 1.5 which is an effective size of 18 (which is the only thing stored in the font). Next, suppose the device's font multiplier changes to 1. So we'd use an RCTFont helper to create a new font based on the existing font but with a scaleMultiplier of 1. However, the font didn't store the font size (12) and scaleMultiplier (1.5) separately. It just knows the (effective) font size of 18. So RCTFont thinks the new font has a font size of 18 and a scaleMultiplier of 1 so its effective font size is 18. This is incorrect and it should have been 12.
To fix this, the font attributes are now all stored individually. Anytime one of them changes, updateFont is called which recreates the font from scratch. This happens to fix some bugs around fontStyle and fontWeight which were reported several times before: #13730, #12738, #2140, #8533.
Test Plan (required)
Created a test app where I verified that
Also, verified switching
Lastly, my team has been using this change in our app.
@rigdern That's amazing! Thank you! Right now I am in the middle of huge refactoring of
I really like the direction of this approach.
But here is how we can do it even better:
- Make a
- Create a new class, something like
RCTFontAttributes(We already have
RCTFontDescriptor, we have to also be sure that we are not reinventing this.)
- RCTFontAttributes should have all font specific props; getter which returns UIFont instance; a delegate with one method
- Have NSNotificationCenter related stuff inside that class;
- Add a prop to RCTTextInput
- Make a RCTTextInput a delegate and implement that method as
self.font = font;;
- Redirect all font specific props in
And then later, finally, we can reuse it inside RCTText and co.
@shergin I didn't have time to test it yet but is my latest commit along the lines that you were thinking?
A tricky detail was that
I wll try to land this in the begginnig of the week!
Thank you so much for your effort!