UIFont+RQExtension.m 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. //
  2. // UIFont+RQExtension.m
  3. // RQCommon
  4. //
  5. // Created by 张嵘 on 2018/11/16.
  6. // Copyright © 2018 张嵘. All rights reserved.
  7. //
  8. /**
  9. *
  10. (
  11. "PingFangSC-Ultralight",
  12. "PingFangSC-Regular",
  13. "PingFangSC-Semibold",
  14. "PingFangSC-Thin",
  15. "PingFangSC-Light",
  16. "PingFangSC-Medium"
  17. )
  18. */
  19. /**
  20. * 极细体
  21. */
  22. static NSString *const RQPingFangSC_Ultralight = @"PingFangSC-Ultralight";
  23. /**
  24. * 常规体
  25. */
  26. static NSString *const RQPingFangSC_Regular = @"PingFangSC-Regular";
  27. /**
  28. * 中粗体
  29. */
  30. static NSString *const RQPingFangSC_Semibold = @"PingFangSC-Semibold";
  31. /**
  32. * 纤细体
  33. */
  34. static NSString *const RQPingFangSC_Thin = @"PingFangSC-Thin";
  35. /**
  36. * 细体
  37. */
  38. static NSString *const RQPingFangSC_Light = @"PingFangSC-Light";
  39. /**
  40. * 中黑体
  41. */
  42. static NSString *const RQPingFangSC_Medium = @"PingFangSC-Medium";
  43. #import "UIFont+RQExtension.h"
  44. @implementation UIFont (RQExtension)
  45. /**
  46. * 苹方极细体
  47. *
  48. * @param fontSize 字体大小
  49. *
  50. */
  51. + (instancetype)rq_fontForPingFangSC_UltralightFontOfSize:(CGFloat)fontSize {
  52. return [UIFont fontWithName:RQPingFangSC_Ultralight size:fontSize];
  53. }
  54. /**
  55. * 苹方常规体
  56. *
  57. * @param fontSize 字体大小
  58. *
  59. */
  60. + (instancetype)rq_fontForPingFangSC_RegularFontOfSize:(CGFloat)fontSize {
  61. return [UIFont fontWithName:RQPingFangSC_Regular size:fontSize];
  62. }
  63. /**
  64. * 苹方中粗体
  65. *
  66. * @param fontSize 字体大小
  67. *
  68. */
  69. + (instancetype)rq_fontForPingFangSC_SemiboldFontOfSize:(CGFloat)fontSize {
  70. return [UIFont fontWithName:RQPingFangSC_Semibold size:fontSize];
  71. }
  72. /**
  73. * 苹方纤细体
  74. *
  75. * @param fontSize 字体大小
  76. *
  77. */
  78. + (instancetype)rq_fontForPingFangSC_ThinFontOfSize:(CGFloat)fontSize {
  79. return [UIFont fontWithName:RQPingFangSC_Thin size:fontSize];
  80. }
  81. /**
  82. * 苹方细体
  83. *
  84. * @param fontSize 字体大小
  85. *
  86. */
  87. + (instancetype)rq_fontForPingFangSC_LightFontOfSize:(CGFloat)fontSize {
  88. return [UIFont fontWithName:RQPingFangSC_Light size:fontSize];
  89. }
  90. /**
  91. * 苹方中黑体
  92. *
  93. * @param fontSize 字体大小
  94. *
  95. */
  96. + (instancetype)rq_fontForPingFangSC_MediumFontOfSize:(CGFloat)fontSize {
  97. return [UIFont fontWithName:RQPingFangSC_Medium size:fontSize];
  98. }
  99. +(void)load {
  100. //获取替换后的类方法
  101. Method newMethod0 = class_getClassMethod([self class], @selector(adjustFont:));
  102. //获取替换前的类方法
  103. Method method0 = class_getClassMethod([self class], @selector(systemFontOfSize:));
  104. //然后交换类方法
  105. method_exchangeImplementations(newMethod0, method0);
  106. //获取替换后的类方法
  107. Method newMethod1 = class_getClassMethod([self class], @selector(adjustFontWithName:size:));
  108. //获取替换前的类方法
  109. Method method1 = class_getClassMethod([self class], @selector(fontWithName:size:));
  110. //然后交换类方法
  111. method_exchangeImplementations(newMethod1, method1);
  112. }
  113. + (UIFont *)adjustFont:(CGFloat)fontSize{
  114. UIFont *newFont = nil;
  115. if (kiPhone6Plus || IS_IPHONE_IPhoneX_All){
  116. newFont = [UIFont adjustFont:fontSize + 2];
  117. }else{
  118. newFont = [UIFont adjustFont:fontSize];
  119. }
  120. return newFont;
  121. }
  122. + (UIFont *)adjustFontWithName:(NSString *)name size:(CGFloat)fontSize {
  123. UIFont *newFont = nil;
  124. if (kiPhone6Plus || IS_IPHONE_IPhoneX_All){
  125. newFont = [UIFont adjustFontWithName:name size:fontSize + 2];
  126. }else{
  127. newFont = [UIFont adjustFontWithName:name size:fontSize];
  128. }
  129. return newFont;
  130. }
  131. @end