iOS - 利用runtime处理font返回nil

创建font的分类,利用runtime中exchange方法实现:

+ (UIFont *)adapt_fontWithName:(NSString *)fontName size:(CGFloat)fontSize {
    if (![UIFont adapt_fontWithName:fontName size:fontSize]) {
        return [UIFont systemFontOfSize:fontSize];
    }
    return [UIFont adapt_fontWithName:fontName size:fontSize];

}
+ (void)load {
    Method fontNameMethod = class_getClassMethod([UIFont class], @selector(fontWithName:size:));
    Method adaptNameMethod = class_getClassMethod([UIFont class], @selector(adapt_fontWithName:size:));
    method_exchangeImplementations(fontNameMethod, adaptNameMethod);
}

猜你喜欢

转载自blog.csdn.net/weixin_33881041/article/details/87089759