font
參考門前有棵葡萄樹
我這里添加一個(gè)用代碼創(chuàng)建的Lable,然后字體大小同樣自適應(yīng)屏幕大小
原理就是給Lable添加一個(gè)類目,利用runtime交換系統(tǒng)和自己定義的方法,在自己的方法中根據(jù)系統(tǒng)來設(shè)置不同的字體(需要考慮不同創(chuàng)建方式).
-
xib
- initWithCoder
- myInitWithCoder
-
代碼
- initWithFrame
- myInitWithFrame
具體initWithCoder和initWithFrame的區(qū)別,我就不說了;以下是類目的.m文件
#import "UILabel+Font.h"
#import <objc/runtime.h>
#define SizeScale ((kScreenHeight > 568) ? kScreenHeight/568 : 1)
@implementation UILabel (Font)
+ (void)load{
//xib創(chuàng)建
Method imp = class_getInstanceMethod([self class], @selector(initWithCoder:));
Method myImp = class_getInstanceMethod([self class], @selector(myInitWithCoder:));
method_exchangeImplementations(imp, myImp);
//代碼initwithframe創(chuàng)建
Method c = class_getInstanceMethod([self class], @selector(initWithFrame:));
Method d = class_getInstanceMethod([self class], @selector(myInitWithFrame:));
method_exchangeImplementations(c, d);
}
#pragma mark - xib創(chuàng)建
- (id)myInitWithCoder:(NSCoder*)aDecode{
[self myInitWithCoder:aDecode];
if (self) {
CGFloat fontSize = self.font.pointSize;
self.font = [UIFont systemFontOfSize:fontSize*SizeScale];
NSLog(@"%f",SizeScale);
}
return self;
}
#pragma mark - 代碼initwithframe創(chuàng)建
- (id)myInitWithFrame:(CGRect )rect {
[self myInitWithFrame:rect];
if (self) {
CGFloat fontSize = self.font.pointSize;
self.font = [UIFont systemFontOfSize:fontSize*SizeScale];
NSLog(@"%f",SizeScale);
}
return self;
}
@end
有不足之處請指出