iOS solves screen adaptation and font adaptation, a small tool is enough!

With the advent of more and more Apple models, the screen adaptation problem that plagued Android developers has now begun to plague Apple developers. How to solve the screen adaptation problem in the simplest way?
This article is mainly aimed at writing ui with the design draft given by the ui designer to ensure that it will not be deformed on any screen. All codes are purely handwritten. Personally, I prefer handwritten ui, because the handwritten ui is consistent with the design draft. Every time the ui changes the design draft, I only need to change a few values ​​like her to ensure that my ui is consistent with the design draft.
When we get the ui design draft, we usually have a reference screen size. For example, our company’s is 750*1334, but the size of each of our models is different. When I got a 480*640 model, the method I could think of was to convert the control that originally had a width of 100 on the 750*1334 model to 100/750. Then multiply it by 480. This will ensure that the control is still in the same position, maintains the same size, and will not be deformed. And when we are on a 960*1800 model, its width only needs to be adjusted to 960*100/750.
So in fact, the value of our new screen can be determined by the following formula
ow: total width of the design draft
oh: total height of the design draft
nw: specific mobile phone width
nh: specific mobile phone height
w: width of specific controls on the design draft
h: above the design draft The height of the home control
adaptedh: the adjusted height on our new phone
adaptedw: the adjusted width on our new phone

adaptedw = w/ow*nw;
adaptedh = h/oh*nh;

Whenever a new screen appears, we only need to replace the last value of the above formula to achieve the requirement of always being in the same position.
Create a new object-c file. exist. The following code is implemented in the h file

#import <Foundation/Foundation.h>

@interface UIUtil : NSObject
@property (nonatomic) float width;
@property (nonatomic) float height;
-(instancetype) initWithWidthAndHeight:(int)width height:(int)height;
-(int)adaptedWidth:(float)width;
-(int)adaptedHeight:(float)height;
@end

This file defines two parameters and three methods. Length, width, the method of initializing the length and width, and the method of obtaining the adjusted length and width.
Next we are. Write their specific implementation in the m file

#import "UIUtil.h"

@implementation UIUtil

-(int)adaptedWidth:(float)width{
    return (int)_width*width/500;
}
-(int)adaptedHeight:(float)height{
    return  (int)_height*height/1000;
}


-(instancetype) initWithWidthAndHeight:(int)width height:(int)height
{
    if(self = [super init]){
        _width=width;
        _height=height;
    }
    return self;
}
@end

The 500 and 1000 here can be replaced with any value of your design draft.
Then when using it, we need to initialize this tool according to the width and height of the current screen, and write this in the place of use

UIUtil * ui = [UIutil alloc] initWithWidthAndHeight:[UIScreen mainScreen].bounds.size.width height:[UIScreen mainScreen].bounds.size.height];

In this way, the tool is successfully initialized, and each time you call it, write according to your design draft

 UIImageView *login = [[UIImageView alloc]initWithFrame:CGRectMake(0, 64+[UI adaptedHeight:104], self.view.frame.size.width, [UI adaptedHeight:250]) ];


[UI adaptedHeight: 104]; that is, the size of 104 on our design draft is adjusted to the size above our current screen ratio. The last thing to note is that the adaptedWidth must be used in the width direction, and the adaptedheight tool must be used in the height direction. It can be used to write frames, and it is also applicable to font size deformation. We know that you need to pass in a value when setting the size of the font. We only need to pass in the value above your design draft, and you can also get what you want Font size
int textsize = [ui adaptedwidth: 24]
This font size is the font size we reset according to the width ratio.
Of course, tools are dead, but people are alive. When we encounter some controls that must maintain the same width and height, we still remember to set the width and height when we initialize. Set the height to be the same, that is, fill in adapted xxx for both width and height

Guess you like

Origin blog.csdn.net/qq_36740186/article/details/77869303