Weex在iOS 13系统手机上,加粗等效果失效问题

手机升级到iOS 13以后,发现之前weex界面的文本加粗效果都不见了。

这是因为在WXTextComponent.mm文件中会使用到CTFontRef为文本设置样式。在该文件的- (NSMutableAttributedString *)buildCTAttributeString方法下,会将UIFont转换为CTFontRef。

在iOS 13以下的手机,打印出的UIFont和CTFontRef一致,没有问题:

Printing description of font:
 font-family: ".SFUIText-Bold"; font-weight: bold; font-style: normal; font-size: 16.00pt
Printing description of ctFont:
 font-family: ".SFUIText-Bold"; font-weight: bold; font-style: normal; font-size: 16.00pt

但在iOS 13手机上,UIFont和CTFontRef为:

Printing description of font:
 font-family: ".SFUI-Bold"; font-weight: bold; font-style: normal; font-size: 16.00pt
Printing description of ctFont:
 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)];
        }
}

你可能感兴趣的:(Weex在iOS 13系统手机上,加粗等效果失效问题)