iOS 开发每天分享优质文章

利用iOS的runtime改变字体

2016-05-26  本文已影响339人  suncheng

在项目开发中有可能遇到一种情况是,项目已经开发到一半或者已经完成了,这时候产品经理说 “来,把APP上的所有字体改成‘xxx’字体”!以下有三种方法可以达成此需求。

继承

可以写一个继承自UIFont的类,在里面自定义一个设置字体的方法,在用到设置字体的时候都调用这个方法就可以了。但这样做有一个明显的缺点:项目里有很地方都要用到设置字体,就需要把所有相应设置字体的方法改成你自定义的方法,工作量巨大,容易出差错。

category

其实要达成这个目的还可以为UIFont建立一个category,重写systemFontOfSize:,覆盖它的原有方法实现,但是这样做就无法调用系统原有的方法,而且苹果也不建议在category中重写方法:

category重写systemFontOfSize.png

会出现上图中警告

所以我们的解决方法就是利用iOS的runtime另写一个方法来和systemFontOfSize:交换。

利用runtime另写一个方法来和systemFontOfSize:交换

主要通过以下面的方法来交换
method_exchangeImplementations(originalMethod, swizzledMethod);

+  (void)load{
 //只执行一次这个方法
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Class class = [self class];
        SEL originalSelector = @selector(systemFontOfSize:);
        SEL swizzledSelector = @selector(mysystemFontOfSize:);
        // Class class = class_getInstanceMethod((id)self);
        Method originalMethod = class_getClassMethod(class, originalSelector);
        Method swizzledMethod = class_getClassMethod(class, swizzledSelector);
        BOOL didAddMethod = class_addMethod(class,originalSelector,
                                            method_getImplementation(swizzledMethod),
                                            method_getTypeEncoding(swizzledMethod));
        if (didAddMethod)
        {
            class_replaceMethod(class,swizzledSelector,
                                method_getImplementation(originalMethod),
                                method_getTypeEncoding(originalMethod));
            
        }
        else
        {
            method_exchangeImplementations(originalMethod, swizzledMethod);
        }
    });
}

IMP是“Implementation”的缩写,它指向一个方法的实现,每个方法都对应一个IMP,我们可以直接调用指针来达到目的。

#import "UIFont+changeFont.h"
#import <objc/runtime.h>
#define CustomFontName @"Menlo-Regular"
typedef id (*_IMP)(id,SEL,...);  // 有返回值
typedef id (*_VIMP)(id,SEL,...); // 无返回值
@implementation UIFont (changeFont)
+(void)load{
static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{ 
         Method systemFontOfSize = class_getClassMethod(self, @selector(systemFontOfSize:));
         _IMP systemFontOfSize_IMP = (_IMP)method_getImplementation(systemFontOfSize);
        method_setImplementation(systemFontOfSize, imp_implementationWithBlock(^(id target,SEL action,CGFloat fontSize){ 
            systemFontOfSize_IMP(target,@selector(systemFontOfSize:));
            UIFont *font = [UIFont fontWithName:CustomFontName size:fontSize];
            return font;
        }));
});
@end

直接调用IMP指针是一种高效简单的方法,以后有类似的问题不妨一试!

上一篇 下一篇

猜你喜欢

热点阅读