SoFunction
Updated on 2025-04-12

Sample code of 3 methods for iOS font size adaptation

Preface

In iOS development, some companies also have adaptation requirements for fonts. In order to make the fonts beautiful, font sizes must also be adapted on screens of different sizes.

I have summarized several methods for your reference. I won’t say much below. Let’s take a look at the detailed introduction together.

The method is as follows:

Method 1: Use macros to define the adaptive font size (judged based on the screen size)

//Macro definition#define SCREEN_WIDTH ([UIScreen mainScreen].)
#define FONT_SIZE(size) ([UIFont systemFontOfSize:FontSize(size))

/**
  * Font adaptation I defined a method in the PCH file
  */
static inline CGFloat FontSize(CGFloat fontSize){
 if (SCREEN_WIDTH==320) {
  return fontSize-2;
 }else if (SCREEN_WIDTH==375){
  return fontSize;
 }else{
  return fontSize+2;
 }
}

Method 2: Use macros to define the adaptive font size (judged based on the screen size)

1.5 represents the font of 6P size 1.5 times, and the size of 5S and 6 sizes are the same, and the proportion can also be customized according to the needs.

The code is as follows:

#define IsIphone6P   SCREEN_WIDTH==414
#define SizeScale   (IsIphone6P ? 1.5 : 1)
#define kFontSize(value) value*SizeScale
#define kFont(value)  [UIFont systemFontOfSize:value*SizeScale]

Method 3: (Use runTime to write classification to UIFont and replace the system-owned method) This is recommended

class_getInstanceMethod gets the instance method of the class

class_getClassMethod to get class class method

1. First, you need to create a UIFont classification

2. The size and width of the phone for the prototype of your own UI design

#define MyUIScreen 375 // UIDesign prototype drawings for mobile phone size width(6), 6pof--414
UIFont+

#import "UIFont+"
#import <objc/>

@implementation UIFont (runtime)

+ (void)load {
 // Get the replaced class method Method newMethod = class_getClassMethod([self class], @selector(adjustFont:));
 // Get the class method before replacement Method method = class_getClassMethod([self class], @selector(systemFontOfSize:));
 // Then exchange the class method and exchange the IMP pointers of the two methods (IMP represents the specific implementation of the method) method_exchangeImplementations(newMethod, method);
}

+ (UIFont *)adjustFont:(CGFloat)fontSize {
 UIFont *newFont = nil;
 newFont = [UIFont adjustFont:fontSize * [UIScreen mainScreen]./MyUIScreen];
 return newFont;
}
@end

Just call the system to set the font method normally outside

ControllerJust call it normally in the class:
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 150, [UIScreen mainScreen]., 60)];
 = @"iOS Font Size Adaptation";
 = [UIFont systemFontOfSize:16];
[ addSubview:label];

Notice:

The load method will only go once, and use the runtime method to replace the method.
In the replacement method (replace the system method with the method we write ourselves), remember to write your own method, otherwise it will be a dead cycle.
After that, wherever the systemFontOfSize method is used, it will be replaced with our own method, and the font size can be changed.
Note: This method can only replace the font size of the control written in pure code. If you use the control created by xib and the font size set in xib, then it cannot be replaced! You need to be in xib
Manually set the control font in the wakeFromNib method

Summarize

The above is the entire content of this article. I hope that the content of this article has a certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.