首页 > 移动平台 > 详细

ios runtime之交换方法method_exchangeImplementations的使用

时间:2018-06-21 19:35:15      阅读:361      评论:0      收藏:0      [点我收藏+]

最常见的情况字体的适配

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:所指向的方法。

ios runtime之交换方法method_exchangeImplementations的使用

原文:https://www.cnblogs.com/jeyios/p/9210512.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!