解决 CoreText note: Client requested name ".SFUI-Regular" warning

前言

我们项目中集成了 unity,unity 版本是 2019.4.16,升级了 iOS 15 后,在真机运行的时候,发现了很多 Waring:

log1.png

有时候在不同系统上面显示的字体不一样,系统的 SFUI 字体会被改成 TimeNewRomanPSMT,也就是非衬线字体改成了衬线字体。

初步推断是由于 2019.4.16 版本的 unity 在支持 iOS 15 上还有些问题,在 iOS 15 之后,SF 会被替换成 TimesNewRomanPSMT ,但是 2019.4.16 版本的 unity 引擎还在使用 SF,当然也有可能是苹果自己的bug。

SF 是 San Francisco 的缩写,是苹果在 2017 推出的一种无衬线字体。

因为一些原因没法升级 unity 引擎版本,所以卑微 iOS 只能自寻出路。

解决

思路也简单,既然这种字体有问题,我们就在使用这种字体的地方,换成另外一种字体即可,一个个换当然麻烦,比较好的办法是直接 hook,那么首先要找到是在什么地方使用。

  1. 首先,添加断点 CTFontLogSystemFontNameRequest
CoreText note: Client requested name ".SFUI-Regular", it will get TimesNewRomanPSMTrather than the intended font. All system UI font access should be through proper APIssuch as CTFontCreateUIFontForLanguage() or +[UIFont systemFontOfSize:].
2021-12-14 19:13:49.929528+0800 NewSS[19078:2375517CoreText note: Set a breakpoint onCTFontLogSystemFontNameRequest to debug.
复制代码

日志也有说明了,添加 CTFontLogSystemFontNameRequest 断点看一看是哪里导致的 warning:

断点2.png

记得是 Symbolic Breakpoint,然后得到运行项目,断点的位置为:

断点3.png

都是汇编层的一些东西,不过也有个 [UIFont fontNamesForFamilyName:] 的 OC 方法,找到这个就好办了。

  1. hook [UIFont fontNamesForFamilyName:]
// 写一个 UIFont 的 Category#import <objc/runtime.h>@implementation UIFont (Add)
​
+ (void)load {
  [super load];
   method_exchangeImplementations(
     class_getClassMethod(self.class,NSSelectorFromString(@"fontNamesForFamilyName:")),
     class_getClassMethod(self.class, @selector(cs_fontNamesForFamilyName:)));
}
​
+ (NSArray<NSString *> *)cs_fontNamesForFamilyName:(NSString *)familyName {
   if (@available(iOS 15.0, *)) {
       if ([familyName isEqualToString:@"System Font"]) {
           familyName = @"Times New Roman";
      }
  }
   return [self cs_fontNamesForFamilyName:familyName];
}
@end
复制代码

打印了一下 familyName

 - 0 : "Academy Engraved LET"
 - 1 : "Al Nile"
 - 2 : "American Typewriter"
 - 3 : "Apple Color Emoji"
 - 4 : "Apple SD Gothic Neo"
 - 5 : "Apple Symbols"
 - 6 : "Arial"
 - 7 : "Arial Hebrew"
 - 8 : "Arial Rounded MT Bold"
 - 9 : "Avenir"
 - 10 : "Avenir Next"
 - 11 : "Avenir Next Condensed"
 - 12 : "Baskerville"
 - 13 : "Bodoni 72"
 - 14 : "Bodoni 72 Oldstyle"
 - 15 : "Bodoni 72 Smallcaps"
 - 16 : "Bodoni Ornaments"
 - 17 : "Bradley Hand"
 - 18 : "Chalkboard SE"
 - 19 : "Chalkduster"
 - 20 : "Charter"
 - 21 : "Cochin"
 - 22 : "Copperplate"
 - 23 : "Courier New"
 - 24 : "Damascus"
 - 25 : "Devanagari Sangam MN"
 - 26 : "Didot"
 - 27 : "DIN Alternate"
 - 28 : "DIN Condensed"
 - 29 : "Euphemia UCAS"
 - 30 : "Farah"
 - 31 : "Futura"
 - 32 : "FZQingKeBenYueSongS-R-GB"
 - 33 : "Galvji"
 - 34 : "Geeza Pro"
 - 35 : "Georgia"
 - 36 : "Gill Sans"
 - 37 : "Grantha Sangam MN"
 - 38 : "Helvetica"
 - 39 : "Helvetica Neue"
 - 40 : "Hiragino Maru Gothic ProN"
 - 41 : "Hiragino Mincho ProN"
 - 42 : "Hiragino Sans"
 - 43 : "Hoefler Text"
 - 44 : "Kailasa"
 - 45 : "Kefa"
 - 46 : "Khmer Sangam MN"
 - 47 : "Kohinoor Bangla"
 - 48 : "Kohinoor Devanagari"
 - 49 : "Kohinoor Gujarati"
 - 50 : "Kohinoor Telugu"
 - 51 : "KometPro-HeavyItalic"
 - 52 : "Lao Sangam MN"
 - 53 : "Malayalam Sangam MN"
 - 54 : "Marker Felt"
 - 55 : "Menlo"
 - 56 : "Mishafi"
 - 57 : "Mukta Mahee"
 - 58 : "Myanmar Sangam MN"
 - 59 : "Noteworthy"
 - 60 : "Noto Nastaliq Urdu"
 - 61 : "Noto Sans Kannada"
 - 62 : "Noto Sans Myanmar"
 - 63 : "Noto Sans Oriya"
 - 64 : "Optima"
 - 65 : "Palatino"
 - 66 : "Papyrus"
 - 67 : "Party LET"
 - 68 : "PingFang HK"
 - 69 : "PingFang SC"
 - 70 : "PingFang TC"
 - 71 : "Rockwell"
 - 72 : "Savoye LET"
 - 73 : "Sinhala Sangam MN"
 - 74 : "Snell Roundhand"
 - 75 : "Symbol"
 - 76 : "System Font"
 - 77 : "Tamil Sangam MN"
 - 78 : "Thonburi"
 - 79 : "Times New Roman"
 - 80 : "Trebuchet MS"
 - 81 : "Verdana"
 - 82 : "Zapf Dingbats"
 - 83 : "Zapfino"
复制代码

经过测试,确认了只要替换 System Font 就不会有这个 Warning,这里我们直接将其替换成 Times New Roman。再次运行,warning 已经没了。

转载请注明 出处

猜你喜欢

转载自juejin.im/post/7041768023031545869