From 89efa1a0c1b633bf9edee66583800ad3fc54ce63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danilo=20B=C3=BCrger?= Date: Fri, 28 Jan 2022 07:36:41 -0800 Subject: [PATCH] Only find closest font if system font was not found (#32482) Summary: Before https://github.com/facebook/react-native/commit/f951da912dd8b4dc2b0d674f8f37f9d982a03c48 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 [iOS] [Fixed] - Respect RCTSetDefaultFontHandler chosen font Pull Request resolved: https://github.com/facebook/react-native/pull/32482 Reviewed By: ShikaSD Differential Revision: D33844138 Pulled By: cortinico fbshipit-source-id: 05c01fc358cd19f8be342218cdba944b303073ed --- React/Views/RCTFont.mm | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/React/Views/RCTFont.mm b/React/Views/RCTFont.mm index 75988e8243e200..679f2cea43ebe2 100644 --- a/React/Views/RCTFont.mm +++ b/React/Views/RCTFont.mm @@ -355,16 +355,18 @@ + (UIFont *)updateFont:(UIFont *)font } } - // Get the closest font that matches the given weight for the fontFamily - CGFloat closestWeight = INFINITY; NSArray *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; + } } } }