preface

Unity is integrated in our project. The Unity version is 2019.4.16. After upgrading iOS 15, we found a lot of Waring when the real machine was running:

Sometimes the fonts displayed on different systems are different, and the SFUI font of the system is changed to TimeNewRomanPSMT, which is a non-serif font changed to serif font.

SF will be replaced with TimesNewRomanPSMT after iOS 15, but unity 2019.4.16 will still use SF. It could also be apple’s own bug.

SF, short for San Francisco, is a sans serif font that Apple introduced in 2017.

For some reason there was no way to upgrade the Unity engine, so humble iOS was left to fend for itself.

To solve

The idea is also simple, since there is a problem with this font, we can change it to another font in the place where this font is used. Of course, it is troublesome to change it one by one. The better way is to hook directly, so first of all, we need to find out where to use it.

  1. First, add a breakpointCTFontLogSystemFontNameRequest
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.
Copy the code

Log also shows, add CTFontLogSystemFontNameRequest breakpoint to take a look at where is caused by warning:

Symbolic Breakpoint, and then get to run the project, Breakpoint location:

All assembler stuff, but there is also a [UIFont fontNamesForFamilyName:] OC method, find this easy to do.

  1. hook [UIFont fontNamesForFamilyName:].
// Write a Category for UIFont#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
Copy the code

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"
Copy the code

After testing, it is confirmed that the Warning will not be generated if the System Font is changed. In this case, we will directly replace it with Times New Roman. Run again, warning is gone.

Please indicate the source of reprint.