美文网首页
动态适配xib 存代码字体

动态适配xib 存代码字体

作者: 15abb039616d | 来源:发表于2021-02-05 15:00 被阅读0次

    .h文件

    #import <UIKit/UIKit.h>
    #import <objc/runtime.h>
    
    NS_ASSUME_NONNULL_BEGIN
    @interface UIFont (FontAdaptive)
    @end
    
    @interface UIButton (FontAdaptive)
    @end
    
    @interface UILabel (FontAdaptive)
    @end
    
    @interface UITextField (FontAdaptive)
    @end
    
    
    @interface UITextView (FontAdaptive)
    
    @end
    NS_ASSUME_NONNULL_END
    

    .m 文件

    #define Adaptive_coefficient [[UIScreen mainScreen]bounds].size.width/375.0f
    
    @implementation UIFont (FontAdaptive)
    
    + (void)load {
        
        Method selfMethod = class_getClassMethod([self class], @selector(runtimeFitFont:));
        Method systenMethod = class_getClassMethod([self class], @selector(systemFontOfSize:));
        method_exchangeImplementations(selfMethod, systenMethod);
    }
    
    + (UIFont *)runtimeFitFont:(CGFloat)fontSize {
        UIFont *fitFont = nil;
        //这里并不会造成递归调用,方法已经被交换
        fitFont = [UIFont runtimeFitFont:fontSize * Adaptive_coefficient];
        return fitFont;
    }
    
    @end
    
    
    @implementation UIButton (FontAdaptive)
    
    + (void)load{
        Method imp = class_getInstanceMethod([self class], @selector(initWithCoder:));
        Method runtimeImp = class_getInstanceMethod([self class], @selector(runtimeInitWithCoder:));
        method_exchangeImplementations(imp, runtimeImp);
    }
    
    - (id)runtimeInitWithCoder:(NSCoder*)aDecode{
        [self runtimeInitWithCoder:aDecode];
        if (self) {
            
            //部分不像改变字体的 把tag值设置成2222跳过
            if(self.titleLabel.tag != 2222){
                CGFloat fontSize = self.titleLabel.font.pointSize;
                self.titleLabel.font = [UIFont systemFontOfSize:fontSize];
            }
        }
        return self;
    }
    
    @end
    
    
    @implementation UILabel (FontAdaptive)
    
    + (void)load{
        Method imp = class_getInstanceMethod([self class], @selector(initWithCoder:));
        Method runtimeImp = class_getInstanceMethod([self class], @selector(runtimeInitWithCoder:));
        method_exchangeImplementations(imp, runtimeImp);
    }
    
    - (id)runtimeInitWithCoder:(NSCoder*)aDecode{
        [self runtimeInitWithCoder:aDecode];
        if (self) {
            //部分不像改变字体的 把tag值设置成2222跳过
            if(self.tag != 2222){
                CGFloat fontSize = self.font.pointSize;
                self.font = [UIFont systemFontOfSize:fontSize];
            }
        }
        return self;
    }
    
    @end
    
    @implementation UITextField (FontAdaptive)
    
    + (void)load{
        Method imp = class_getInstanceMethod([self class], @selector(initWithCoder:));
        Method runtimeImp = class_getInstanceMethod([self class], @selector(runtimeInitWithCoder:));
        method_exchangeImplementations(imp, runtimeImp);
    }
    
    - (id)runtimeInitWithCoder:(NSCoder*)aDecode{
        [self runtimeInitWithCoder:aDecode];
        if (self) {
            //部分不像改变字体的 把tag值设置成2222跳过
            if(self.tag != 2222){
                CGFloat fontSize = self.font.pointSize;
                self.font = [UIFont systemFontOfSize:fontSize];
            }
        }
        return self;
    }
    
    @end
    
    @implementation UITextView (FontAdaptive)
    
    + (void)load{
        Method imp = class_getInstanceMethod([self class], @selector(initWithCoder:));
        Method runtimeImp = class_getInstanceMethod([self class], @selector(runtimeInitWithCoder:));
        method_exchangeImplementations(imp, runtimeImp);
    }
    
    - (id)runtimeInitWithCoder:(NSCoder*)aDecode{
        [self runtimeInitWithCoder:aDecode];
        if (self) {
            //部分不像改变字体的 把tag值设置成2222跳过
            if(self.tag != 2222){
                CGFloat fontSize = self.font.pointSize;
                self.font = [UIFont systemFontOfSize:fontSize];
            }
        }
        return self;
    }
    
    @end
    
    

    相关文章

      网友评论

          本文标题:动态适配xib 存代码字体

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