我如何在 IOS13 中找到这个与字体相关的 coretext 警告的来源?

How can i find the source of this font-related coretext warning in IOS13?

我正在更新我的应用程序,我注意到当 运行 IOS13 11.2 中的应用程序 IOS13 时,我在日志中收到大量警告。

CoreText note: Client requested name ".SFUI-Regular", it will get TimesNewRomanPSMT rather than the intended font. All system UI font access should be through proper APIs such as CTFontCreateUIFontForLanguage() or +[UIFont systemFontOfSize:].

我仔细研究了一下,发现了 WWDC 的这句话:

As mentioned in numerous WWDC sessions, dot-prefixed font names are not to be directly used.

我自己几乎完全使用 IB 和 nibs 为文本字段等设置字体,并且在我的代码中任何地方都没有引用 "SFUI-Regular",所以我不确定如何找到真正的原因这些警告(我在日志中有 20-30 行这样的警告)。

有人对我如何找到警告的来源以及如何修复它有任何提示吗?

console还有一个输出,你可以试试加个符号断点

CoreText note: Set a breakpoint on CTFontLogSystemFontNameRequest to debug.

有同样的问题,我的代码中也没有引用点前缀字体。设置一个符号断点但没有任何用处

对我来说,原来是第三方库有一段时间没有更新才是罪魁祸首。

我按照用户clatt的建议下了断点,找到了源码。在我的例子中是 TOMSMorphingLabel.

我开始在控制台中遇到此警告,从 Xcode 11 开始,MacOS 和 iOS 目标。

您将从 UIFont.systemFont(ofSize: X).fontName 收到“.SFUI-Regular”。 如果您尝试使用 UIFont(name: fontName, size: size).

进行实例化,则会出现警告

在我的例子中,我让用户自定义显示字体,但默认为“.SFUI-Regular”,所以我将其更改为 "TimesNewRomanPSMT"

let defaultFont = UIFont.systemFont(ofSize: X).fontName

// replace with
let defaultFont = "TimesNewRomanPSMT"

UIFont(name: defaultFont, size: size)
let fontCT = CTFontCreateUIFontForLanguage(.label, fontSize as CGFloat, nil)
attrStr.addAttribute(.font, value: fontCT as Any, range: NSMakeRange(0, text.count))

ios 13

uifont 问题的解决方案

如果我盲目使用,我会得到这个:

UIFont(名称: fontName, 大小: fontSize)

fontName 是“系统字体”,它是我从中获得的列表的一部分:

让 fontFamilyNames = UIFont.familyNames.sorted()

解决方法是检查名称。

let isSystemFont = fontName == "System Font"
let useFont = isSystemFont ? UIFont.systemFont(ofSize: fontSize) : UIFont(name: fontName, size: fontSize) ?? UIFont.systemFont(ofSize: fontSize)