iOS字体大小自动适配

扩展UIfont,然后使用Runtime替换方法

#import "UIFont+YXGFont.h"
#import <objc/runtime.h>

#define SCREEN_WIDTH 375  //默认屏幕宽度

@implementation UIFont (YXGFont)

+(void)load{
    //获取替换后的类方法
    Method newMethod = class_getClassMethod([self class], @selector(adjustFontSize:));
    //获取需要替换的类方法
    Method method = class_getClassMethod([self class], @selector(systemFontOfSize:));
    //交换方法
    method_exchangeImplementations(newMethod, method);
    
}

+(UIFont *)adjustFontSize:(CGFloat)fontSize{
    UIFont *newFont=nil;
    newFont = [UIFont adjustFont:fontSize * [UIScreen mainScreen].bounds.size.width/SCREEN_WIDTH];
    return newFont;
}

@end
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容