代碼適配
#import "UIFont+DXFont.h"
#import <objc/runtime.h>
#define MyUIScreen 375 // UI設(shè)計原型圖的手機尺寸寬度(6s), 6p的--414
@implementation UIFont (DXFont)
+ (void)load{
//獲取替換后的類方法
Method newMethod = class_getClassMethod([self class], @selector(adjustFont:));
//獲取替換前的類方法
Method method = class_getClassMethod([self class], @selector(systemFontOfSize:));
//然后交換類方法
method_exchangeImplementations(newMethod, method);
}
// 以6s未基準(zhǔn)(因為一般UI設(shè)計是以6s尺寸為基準(zhǔn)設(shè)計的)的字體伸眶。在5s和6P上會根據(jù)屏幕尺寸堡牡,字體大小會相應(yīng)的擴大和縮小
+ (UIFont *)adjustFont:(CGFloat)fontSize {
UIFont *newFont = nil;
newFont = [UIFont adjustFont:fontSize * [UIScreen mainScreen].bounds.size.width/MyUIScreen];
return newFont;
}
@end
xib適配
#import "UILabel+DXFont.h"
#import <objc/runtime.h>
@implementation UILabel (DXFont)
+ (void)load{
//利用running time運行池的方法在程序啟動的時候把兩個方法替換 適用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) {
CGFloat fontSize = self.font.pointSize;
self.font = [UIFont systemFontOfSize:fontSize];
}
return self;
}
@end
#import "UIButton+DXFont.h"
#import <objc/runtime.h>
@implementation UIButton (DXFont)
+ (void)load{
//利用running time運行池的方法在程序啟動的時候把兩個方法替換 適用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) {
CGFloat fontSize = self.titleLabel.font.pointSize;
self.titleLabel.font = [UIFont systemFontOfSize:fontSize];
}
return self;
}
@end
參考文章:
http://www.reibang.com/p/8b8731ed84f8
http://www.reibang.com/p/0541caf7cecb