UIFont+Adapt.m 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. //
  2. // UIFont+Adapt.m
  3. //
  4. // Created by YoMi on 2022/6/21.
  5. //
  6. #import "UIFont+Adapt.h"
  7. #import <objc/runtime.h>
  8. @implementation UIFont (Adapt)
  9. +(void)load{
  10. static dispatch_once_t onceToken;
  11. dispatch_once(&onceToken, ^{
  12. //获取系统的方法
  13. /** 系统字体*/
  14. Method OriginalSystemFont = class_getClassMethod([self class], @selector(systemFontOfSize:));
  15. Method NewSystemFont = class_getClassMethod([self class], @selector(ym_systemFontOfSize:));
  16. method_exchangeImplementations(NewSystemFont, OriginalSystemFont);
  17. /** 加粗系统字体*/
  18. Method OriginalBoldSystemFont = class_getClassMethod([self class], @selector(boldSystemFontOfSize:));
  19. Method NewBoldSystemFont = class_getClassMethod([self class], @selector(ym_boldSystemFontOfSize:));
  20. method_exchangeImplementations(NewBoldSystemFont, OriginalBoldSystemFont);
  21. /** 自定义字体*/
  22. Method OriginalCustomFont = class_getClassMethod([self class], @selector(fontWithName:size:));
  23. Method NewCustomFont = class_getClassMethod([self class], @selector(ym_fontWithName:size:));
  24. method_exchangeImplementations(NewCustomFont, OriginalCustomFont);
  25. NSLog(@"替换字体方法中");
  26. });
  27. }
  28. +(UIFont *)ym_systemFontOfSize:(CGFloat)fontSize{
  29. return [UIFont ym_systemFontOfSize:fontSize*[UIScreen mainScreen].bounds.size.width /375 ];
  30. }
  31. +(UIFont *)ym_boldSystemFontOfSize:(CGFloat)fontSize{
  32. return [UIFont ym_boldSystemFontOfSize:fontSize*[UIScreen mainScreen].bounds.size.width /375 ];
  33. }
  34. +(UIFont *)ym_fontWithName:(NSString *)fontName size:(CGFloat)fontSize{
  35. return [UIFont ym_fontWithName:fontName size:fontSize*[UIScreen mainScreen].bounds.size.width /375 ];
  36. }
  37. @end