12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- //
- // UIFont+Adapt.m
- //
- // Created by YoMi on 2022/6/21.
- //
- #import "UIFont+Adapt.h"
- #import <objc/runtime.h>
- @implementation UIFont (Adapt)
- +(void)load{
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- //获取系统的方法
- /** 系统字体*/
- Method OriginalSystemFont = class_getClassMethod([self class], @selector(systemFontOfSize:));
- Method NewSystemFont = class_getClassMethod([self class], @selector(ym_systemFontOfSize:));
- method_exchangeImplementations(NewSystemFont, OriginalSystemFont);
- /** 加粗系统字体*/
- Method OriginalBoldSystemFont = class_getClassMethod([self class], @selector(boldSystemFontOfSize:));
- Method NewBoldSystemFont = class_getClassMethod([self class], @selector(ym_boldSystemFontOfSize:));
- method_exchangeImplementations(NewBoldSystemFont, OriginalBoldSystemFont);
- /** 自定义字体*/
- Method OriginalCustomFont = class_getClassMethod([self class], @selector(fontWithName:size:));
- Method NewCustomFont = class_getClassMethod([self class], @selector(ym_fontWithName:size:));
- method_exchangeImplementations(NewCustomFont, OriginalCustomFont);
- NSLog(@"替换字体方法中");
- });
- }
- +(UIFont *)ym_systemFontOfSize:(CGFloat)fontSize{
- return [UIFont ym_systemFontOfSize:fontSize*[UIScreen mainScreen].bounds.size.width /375 ];
- }
- +(UIFont *)ym_boldSystemFontOfSize:(CGFloat)fontSize{
- return [UIFont ym_boldSystemFontOfSize:fontSize*[UIScreen mainScreen].bounds.size.width /375 ];
- }
- +(UIFont *)ym_fontWithName:(NSString *)fontName size:(CGFloat)fontSize{
- return [UIFont ym_fontWithName:fontName size:fontSize*[UIScreen mainScreen].bounds.size.width /375 ];
- }
- @end
|