在iOS8遇到PingFangSC不存在的情況,通過[UIFont fontWithName:fontName size:fontSize]生成的對象是nil,再調用NSMutableAttributedString的addAttribute會造成crash。
以下生成UIFont對象的安全寫法
+ (UIFont *)fontWithName:(NSString *)fontName size:(float)fontSize
{
UIFont *result = [UIFont fontWithName:fontName size:fontSize];
if (!result) {
result = [UIFont systemFontOfSize:fontSize];
}
return result;
}