手机升级到iOS 13以后,发现之前weex界面的文本加粗效果都不见了。
这是因为在WXTextComponent.mm
文件中会使用到CTFontRef
为文本设置样式。在该文件的- (NSMutableAttributedString *)buildCTAttributeString
方法下,会将UIFont转换为CTFontRef。
在iOS 13以下的手机,打印出的UIFont和CTFontRef一致,没有问题:
Printing description of font:
<UICTFont: 0x1036db630> font-family: ".SFUIText-Bold"; font-weight: bold; font-style: normal; font-size: 16.00pt
Printing description of ctFont:
<UICTFont: 0x103653e90> font-family: ".SFUIText-Bold"; font-weight: bold; font-style: normal; font-size: 16.00pt
但在iOS 13手机上,UIFont和CTFontRef为:
Printing description of font:
<UICTFont: 0x10fea4730> font-family: ".SFUI-Bold"; font-weight: bold; font-style: normal; font-size: 16.00pt
Printing description of ctFont:
<UICTFont: 0x10fea4920> font-family: "Times New Roman"; font-weight: normal; font-style: normal; font-size: 16.00pt
同时可以看到控制台会输出以下日志:
CoreText performance note: Client called CTFontCreateWithName() using name ".SFUI-Regular" and got font with PostScript name "TimesNewRomanPSMT". For best performance, only use PostScript names when calling this API.
可见,在iOS 13上的font-family名发生了变化,导致UIFont转为CTFontRef时的错误。所有可以将设置AttributeString的font的代码修改如下即可:
if (WX_SYS_VERSION_LESS_THAN(@"13.0")) {
CTFontRef ctFont = CTFontCreateWithName((__bridge CFStringRef)font.fontName,
font.pointSize,
NULL);
if (ctFont) {
[attributedString addAttribute:(id)kCTFontAttributeName value:(__bridge id)(ctFont) range:NSMakeRange(0, string.length)];
CFRelease(ctFont);
}
} else {
if (font) {
[attributedString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, string.length)];
}
}
网友评论