1.在不同的手機(jī)上對(duì)應(yīng)的字體大小可能不同,一般的來(lái)說(shuō)在 iphone 4 5 6的手機(jī)上的字體是一樣大小,在6P上的字體是4 5 6上的1.5倍,
下面進(jìn)行l(wèi)abel和button的字體適配
1.先創(chuàng)建label的延展
#import <UIKit/UIKit.h>
#import <objc/runtime.h>
@interface UILabel (DXFont)
@end
#import "UILabel+DXFont.h"
@implementation UILabel (DXFont)
+ (void)load{
//利用running time運(yùn)行池的方法在程序啟動(dòng)的時(shí)候把兩個(gè)方法替換 適用Xib建立的label
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è)置成LabelFontSize值的跳過(guò)
if (IS_IPHONE_6P) {
if(self.tag != LabelFontSize) {
CGFloat fontSize = self.font.pointSize;
self.font = [UIFont systemFontOfSize:fontSize*1.5];
}
}
}
return self;
}
2.button的實(shí)現(xiàn)
#import <UIKit/UIKit.h>
#import <objc/runtime.h>
@interface UIButton (DXFont)
@end
#import "UIButton+DXFont.h"
@implementation UIButton (DXFont)
+ (void)load{
//利用running time運(yùn)行池的方法在程序啟動(dòng)的時(shí)候把兩個(gè)方法替換 適用Xib建立的label
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è)置成2016跳過(guò)
if (IS_IPHONE_6P) {
if(self.tag != 2016) {
CGFloat fontSize = self.titleLabel.font.pointSize;
self.titleLabel.font = [UIFont systemFontOfSize:fontSize*1.5];
}
}
}
return self;
}
如果要適配根據(jù)不同的屏幕使用不同的尺寸把后面的1.5 換成 下面定義的宏就可以啦
//不同設(shè)備的屏幕比例(當(dāng)然倍數(shù)可以自己控制)
#define SizeScale ((IPHONE_HEIGHT > 568) ? IPHONE_HEIGHT/568 : 1)