美文网首页ios
iOS项目文件字体统一调大

iOS项目文件字体统一调大

作者: 会游泳de鱼 | 来源:发表于2016-09-20 10:36 被阅读558次

    今天项目做完了,测试经理说iOS版本的字体太小,要同意调大(——),欲哭无泪呀,难道要熬夜一个个label改,我的天,我的天,我的天(------)*


    废话不多说了,讲下实现原理吧(肯定不是一个个改啦):

    创建一个label的分类,然后利用runtime机制替换init,initWithFrame:,awakeFromNib这三个方法,然后在这个方法里修改font就行了,是不是很简单


    代码块

    步骤1:添加runtime文件

    #import <objc/runtime.h>

    +(void)load {
        //只执行一次这个方法
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            Class class = [self class];
            // When swizzling a class method, use the following:
            // Class class = object_getClass((id)self);
            //替换三个方法
            SEL originalSelector = @selector(init);
            SEL originalSelector2 = @selector(initWithFrame:);
            SEL originalSelector3 = @selector(awakeFromNib);
            SEL swizzledSelector = @selector(YHBaseInit);
            SEL swizzledSelector2 = @selector(YHBaseInitWithFrame:);
            SEL swizzledSelector3 = @selector(YHBaseAwakeFromNib);
            Method originalMethod = class_getInstanceMethod(class, originalSelector);
            Method originalMethod2 = class_getInstanceMethod(class, originalSelector2);
            Method originalMethod3 = class_getInstanceMethod(class, originalSelector3);
            Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
            Method swizzledMethod2 = class_getInstanceMethod(class, swizzledSelector2);
            Method swizzledMethod3 = class_getInstanceMethod(class, swizzledSelector3);
            BOOL didAddMethod =
            class_addMethod(class,
                            originalSelector,
                            method_getImplementation(swizzledMethod),
                            method_getTypeEncoding(swizzledMethod));
            BOOL didAddMethod2 =
            class_addMethod(class,
                            originalSelector2,
                            method_getImplementation(swizzledMethod2),
                            method_getTypeEncoding(swizzledMethod2));
            BOOL didAddMethod3 =
            class_addMethod(class,
                            originalSelector3,
                            method_getImplementation(swizzledMethod3),
                            method_getTypeEncoding(swizzledMethod3)); 
            if (didAddMethod) {
                class_replaceMethod(class,
                                    swizzledSelector,
                                    method_getImplementation(originalMethod),
                                    method_getTypeEncoding(originalMethod));
                
            } else {
                method_exchangeImplementations(originalMethod, swizzledMethod);
            }
            if (didAddMethod2) {
                class_replaceMethod(class,
                                    swizzledSelector2,
                                    method_getImplementation(originalMethod2),
                                    method_getTypeEncoding(originalMethod2));
            }else {
                method_exchangeImplementations(originalMethod2, swizzledMethod2);
            }
            if (didAddMethod3) {
                class_replaceMethod(class,
                                    swizzledSelector3,
                                    method_getImplementation(originalMethod3),
                                    method_getTypeEncoding(originalMethod3));
            }else {
                method_exchangeImplementations(originalMethod3, swizzledMethod3);
            }
        });   }
    
    /**
     *在这些方法中将你的字体名字换进去
     */
    - (instancetype)YHBaseInit {
        id __self = [self YHBaseInit];
        UIFont * font = [UIFont fontWithName:@"Arial-BoldItalicMT" size:self.font.pointSize+20];
        if (font) {
            self.font= [font fontWithSize:self.font.pointSize+30];
        }
        return __self;
    }
    
    -(instancetype)YHBaseInitWithFrame:(CGRect)rect{
        id __self = [self YHBaseInitWithFrame:rect];
        UIFont * font = [UIFont fontWithName:@"Arial-BoldItalicMT" size:self.font.pointSize+20];
        if (font) {
            self.font=font;
             self.font= [font fontWithSize:self.font.pointSize+30];
        }
        return __self;
    }
    
    -(void)YHBaseAwakeFromNib{
        [self YHBaseAwakeFromNib];
        UIFont * font = [UIFont fontWithName:@"Arial-BoldItalicMT" size:self.font.pointSize+20];
        if (font) {
            self.font=font;
            self.font= [font fontWithSize:self.font.pointSize+30];
        }
        
    }```
    >这个分类可以不仅可以用于全体修改文字,还可以全体修改文字的类型

    相关文章

      网友评论

        本文标题:iOS项目文件字体统一调大

        本文链接:https://www.haomeiwen.com/subject/ratoettx.html