:Several methods of iOS font size adaptation-empirical point of view免费ppt模版下载-道格办公

Several methods of iOS font size adaptation

iOS font size adaptation is usually to ensure that the font display effect of the application is consistent on different devices. The following are several common iOS font size adaptation methods: 1. Dynamic font size: iOS provides a dynamic font size fea

< span >The content of this official account is reposted by this account, and the source has been indicated as much as possible. If you fail to verify the source or forward the content and pictures with copyright defects, please contact this account in time, and this account will modify or delete it as soon as possible. QQ: 3442093904

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

I have summarized several methods for your reference.

Method 1: Use macro definition to adapt font size (judged according to screen size)

//macro definition
#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
# 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) {< br> return fontSize-2;
}else if (SCREEN_WIDTH==375){
return fontSize;
}else{
return fontSize+< span class='hljs-number' >2;
}
}


Method 2: Use macro definition to adapt font size (judged according to screen size)

1.5 means that the font size is 1.5 times that of the 6P size, and the size of the 5S and 6 sizes is the same. You can also customize the ratio according to your 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 categories to UIFont to replace the method that comes with the system) It is recommended to use this


class_getInstanceMethod gets the instance method of the class

class_getClassMethod gets the class method of the class

  1. First you need to create a UIFont category

  2. Mobile phone size and width of your own UI design prototype

#define MyUIScreen 375 // The mobile phone size width of the UI design prototype (6), 6p--414


UIFont+runtime.m

#import 'UIFont+runtime.h '
#import <objc/runtime.h>

@implementation UIFont ( runtime)

+ (void)load {
< span class='hljs-comment' >// Get the replaced class method

Method newMethod = class_getClassMethod([self class], @selector(adjustFont:));
// Get Class method before replacement
Method method = class_getClassMethod([self class], @selector(systemFontOfSize:));
// Then exchange the class method, exchange the IMP pointer 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].bounds.size.width/MyUIScreen];
return newFont;
}
@end


Externally call the system to set the font method normally

Just call it normally in the Controller class:

UILabel *label = [[ UILabel alloc]initWithFrame:CGRectMake(0, 150, [UIScreen mainScreen].bounds.size.width, 60)]; 
label.text = @'iOS font size adaptation';
label.font = [UIFont< /span> systemFontOfSize:16];
[self.view addSubview:label];


Note:

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 our own method), remember to write your own method here, otherwise there will be an endless loop. Afterwards, wherever the systemFontOfSize method is used, it will be replaced by 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 create a control with xib and set the font size in xib, then it cannot be replaced! You need to manually set the control font in the awakeFromNib method of xib

Author: Dark Blue_S

Link: https://www.jianshu.com/p/7a6106f952d3

Articles are uploaded by users and are for non-commercial browsing only. Posted by: Lomu, please indicate the source: https://www.daogebangong.com/en/articles/detail/Several%20methods%20of%20iOS%20font%20size%20adaptation.html

Like (810)
Reward 支付宝扫一扫 支付宝扫一扫
single-end

Related Suggestion