ios runtime之交换方法method_exchangeImplementations的使用

最常见的情况字体的适配

UIFont新建分类重写Load方法

#define SCALE(s)  ((s) / 375.0 * SCREEN_WIDTH)

+ (void)load {

    // 自己的方法

    Method newMethod =class_getClassMethod([self class], @selector(adjustFont:));

    // 系统的方法

    Method method = class_getClassMethod([self class], @selector(systemFontOfSize:));

    // 黑魔法交换方法

    method_exchangeImplementations(newMethod, method);

}

+(UIFont *)adjustFont:(CGFloat)fontSize{

    UIFont *newFont=nil;

    newFont = [UIFont adjustFont:SCALE(fontSize)];

    return newFont;

}

当然这个方法网上一搜一大推,但是重要的是思路,用自己的方法和系统的方法交换其实变成了adjustFont:实现的是systemFontOfSize:所指向的方法。

猜你喜欢

转载自www.cnblogs.com/jeyios/p/9210512.html