Skip to content

Commit

Permalink
Only find closest font if system font was not found
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
danilobuerger authored Oct 27, 2021
1 parent fdd744f commit 0c5d6e6
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 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;
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;
}
}
}
}
Expand Down

0 comments on commit 0c5d6e6

Please sign in to comment.