From 6176aa4bc9c3f685ee8358f078ec3d714bb02e79 Mon Sep 17 00:00:00 2001 From: Rahul Malik Date: Thu, 11 Jul 2019 11:54:33 -0700 Subject: [PATCH] Bridge CTFontRef directly to UIFont when converting attributes on NSAttributedString (#1579) In iOS13, there is an issue with the font returned from "-[UIFontj fontWithName:size:]". When asking for "SFUI-Regular" returns "Times New Roman" font. It isn't clear why we were trying to create a new font instance when CTFontRef toll-free bridges to UIFont. This appears to pass tests both internally at Pinterest and Texture so I feel like the change is pretty safe and should also solve the iOS 13 regression users are experiencing. --- Source/TextKit/ASTextKitCoreTextAdditions.mm | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/Source/TextKit/ASTextKitCoreTextAdditions.mm b/Source/TextKit/ASTextKitCoreTextAdditions.mm index e98b236c..df3df696 100644 --- a/Source/TextKit/ASTextKitCoreTextAdditions.mm +++ b/Source/TextKit/ASTextKitCoreTextAdditions.mm @@ -68,16 +68,9 @@ NSDictionary *NSAttributedStringAttributesForCoreTextAttributes(NSDictionary *co // kCTFontAttributeName -> NSFontAttributeName if ([coreTextKey isEqualToString:(NSString *)kCTFontAttributeName]) { + // Its reference type, CTFontRef, is toll-free bridged with UIFont in iOS and NSFont in OS X CTFontRef coreTextFont = (__bridge CTFontRef)coreTextValue; - NSString *fontName = (__bridge_transfer NSString *)CTFontCopyPostScriptName(coreTextFont); - CGFloat fontSize = CTFontGetSize(coreTextFont); - UIFont *font = [UIFont fontWithName:fontName size:fontSize]; - ASDisplayNodeCAssertNotNil(font, @"unable to load font %@ with size %f", fontName, fontSize); - if (font == nil) { - // Gracefully fail if we were unable to load the font. - font = [UIFont systemFontOfSize:fontSize]; - } - cleanAttributes[NSFontAttributeName] = font; + cleanAttributes[NSFontAttributeName] = (__bridge UIFont *)coreTextFont; } // kCTKernAttributeName -> NSKernAttributeName else if ([coreTextKey isEqualToString:(NSString *)kCTKernAttributeName]) {