一個(gè)iOS開發(fā)項(xiàng)目無外乎就是純代碼布局、xib或SB布局。那么如何實(shí)現(xiàn)兩個(gè)方式的字體大小適配呢?
字體大小適配------純代碼
定義一個(gè)宏定義如下
#define SizeScale (SCREEN_WIDTH == 414 ? 1 : 0.83)(我用的7P當(dāng)做1)
#define SizeScale (SCREEN_WIDTH != 414 ? 1 : 1.2)
#define kFont(value) [UIFont systemFontOfSize:value * SizeScale]
在宏中的1.2是在plus下的大小放大比例,可以根據(jù)自己的實(shí)際情況來調(diào)整。
純代碼中設(shè)置字體大小通過使用這個(gè)宏來實(shí)現(xiàn)整體適配
字體大小適配------xib或SB
字體大小適配無外乎就是設(shè)置UIButton、UILabel、UITextView、UITextField的字體大小
通過創(chuàng)建這幾個(gè)的類目來實(shí)現(xiàn)(Runtime方式的黑魔法method swizzling)
廢話不多說,直接上代碼:
.h
#import <UIKit/UIKit.h>
#import <objc/runtime.h>
/**
* button
*/
@interface UIButton (MyFont)
@end
/**
* Label
*/
@interface UILabel (myFont)
@end
/**
* TextField
*/
@interface UITextField (myFont)
@end
/**
* TextView
*/
@interface UITextView (myFont)
@end
.m
#import "UIButton+MyFont.h"
//不同設(shè)備的屏幕比例(當(dāng)然倍數(shù)可以自己控制)
#define SizeScale (SCREEN_WIDTH == 414 ? 1 : 0.83) // 1:1.2
@implementation UIButton (MyFont)
+ (void)load{
Method imp = class_getInstanceMethod([self class], @selector(initWithCoder:));
Method myImp = class_getInstanceMethod([self class], @selector(myInitWithCoder:));
method_exchangeImplementations(imp, myImp);
}
- (id)myInitWithCoder:(NSCoder*)aDecode{
[self myInitWithCoder:aDecode];
if (self) {
//部分不像改變字體的 把tag值設(shè)置成333跳過
if(self.titleLabel.tag != 333){
CGFloat fontSize = self.titleLabel.font.pointSize;
self.titleLabel.font = [UIFont systemFontOfSize:fontSize * SizeScale];
}
}
return self;
}
@end
@implementation UILabel (myFont)
+ (void)load{
Method imp = class_getInstanceMethod([self class], @selector(initWithCoder:));
Method myImp = class_getInstanceMethod([self class], @selector(myInitWithCoder:));
method_exchangeImplementations(imp, myImp);
}
- (id)myInitWithCoder:(NSCoder*)aDecode{
[self myInitWithCoder:aDecode];
if (self) {
//部分不像改變字體的 把tag值設(shè)置成333跳過
if(self.tag != 333){
CGFloat fontSize = self.font.pointSize;
self.font = [UIFont systemFontOfSize:fontSize * SizeScale];
}
}
return self;
}
@end
@implementation UITextField (myFont)
+ (void)load{
Method imp = class_getInstanceMethod([self class], @selector(initWithCoder:));
Method myImp = class_getInstanceMethod([self class], @selector(myInitWithCoder:));
method_exchangeImplementations(imp, myImp);
}
- (id)myInitWithCoder:(NSCoder*)aDecode{
[self myInitWithCoder:aDecode];
if (self) {
//部分不像改變字體的 把tag值設(shè)置成333跳過
if(self.tag != 333){
CGFloat fontSize = self.font.pointSize;
self.font = [UIFont systemFontOfSize:fontSize * SizeScale];
}
}
return self;
}
@end
@implementation UITextView (myFont)
+ (void)load{
Method imp = class_getInstanceMethod([self class], @selector(initWithCoder:));
Method myImp = class_getInstanceMethod([self class], @selector(myInitWithCoder:));
method_exchangeImplementations(imp, myImp);
}
- (id)myInitWithCoder:(NSCoder*)aDecode{
[self myInitWithCoder:aDecode];
if (self) {
//部分不像改變字體的 把tag值設(shè)置成333跳過
if(self.tag != 333){
CGFloat fontSize = self.font.pointSize;
self.font = [UIFont systemFontOfSize:fontSize * SizeScale];
}
}
return self;
}
@end