Skip to content

Commit

Permalink
Only find closest font if system font was not found (#32482)
Browse files Browse the repository at this point in the history
Summary:
Before f951da9 finding a system font used to return early. In order to allow variants, the referenced patch removed the early return so that variants could be applied later. However, there is no need to find the closest font as we already selected the proper system font. This also fixes a bug with setting a custom font handler via RCTSetDefaultFontHandler whos return could get overwritten by the closest font search.

## Changelog

<!-- Help reviewers and the release process by writing your own changelog entry. For an example, see:
https://github.com/facebook/react-native/wiki/Changelog
-->

[iOS] [Fixed] - Respect RCTSetDefaultFontHandler chosen font

Pull Request resolved: #32482

Reviewed By: ShikaSD

Differential Revision: D33844138

Pulled By: cortinico

fbshipit-source-id: 05c01fc358cd19f8be342218cdba944b303073ed
  • Loading branch information
danilobuerger authored and facebook-github-bot committed Jan 28, 2022
1 parent 384e1a0 commit 89efa1a
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions React/Views/RCTFont.mm
Original file line number Diff line number Diff line change
Expand Up @@ -355,16 +355,18 @@ + (UIFont *)updateFont:(UIFont *)font
}
}

// Get the closest font that matches the given weight for the fontFamily
CGFloat closestWeight = INFINITY;
NSArray<NSString *> *names = fontNamesForFamilyName(familyName);
for (NSString *name in names) {
UIFont *match = [UIFont fontWithName:name size:fontSize];
if (isItalic == isItalicFont(match) && isCondensed == isCondensedFont(match)) {
CGFloat testWeight = weightOfFont(match);
if (ABS(testWeight - fontWeight) < ABS(closestWeight - fontWeight)) {
font = match;
closestWeight = testWeight;
if (!didFindFont) {
// Get the closest font that matches the given weight for the fontFamily
CGFloat closestWeight = INFINITY;
for (NSString *name in names) {
UIFont *match = [UIFont fontWithName:name size:fontSize];
if (isItalic == isItalicFont(match) && isCondensed == isCondensedFont(match)) {
CGFloat testWeight = weightOfFont(match);
if (ABS(testWeight - fontWeight) < ABS(closestWeight - fontWeight)) {
font = match;
closestWeight = testWeight;
}
}
}
}
Expand Down

0 comments on commit 89efa1a

Please sign in to comment.