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
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容