開發(fā)中所涉及的屏幕適配無非2種,控件大小的適配和字體大小的適配。
1. 控件大小的適配
1.1 豎屏下的宏,用來設(shè)置控件frame
#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
#define IsIphone6P KScreenSize.width==414
#define IsIphone6 KScreenSize.width==375
#define IsIphone5S KScreenSize.height==568
#define KIphoneSize_Width(value) (IsIphone6P?1.104*value : (IsIphone6?value:(IsIphone5S ? 0.853*value :0.853*value)))
#define KIphoneSize_Height(value) (IsIphone6P?1.103*value:(IsIphone6?value:(IsIphone5S?0.851*value:0.720*value)))
1.2 橫屏下的宏,用來設(shè)置控件frame
#define IsHorIphone6P KScreenSize.height==414
#define IsHorIphone6 KScreenSize.height==375
#define IsHorIphone5S KScreenSize.height==320
#define KIphoneSizeHor_Width(value) (IsHorIphone6P?1.103*value : (IsHorIphone6?value:(IsHorIphone5S?0.851*value:0.720*value)))
#define KIphoneSizeHor_Height(value) (IsHorIphone6P?1.104*value : (IsHorIphone6?value:(IsHorIphone5S?0.853*value:0.853*value)))
2. 字體大小的適配
開發(fā)項目2種方式布局:純代碼,xib和storyBoard,所以2種適配方式。
2.1 純代碼布局-宏定義
1.2代表6P尺寸的時候字體為1.2倍,5S和6尺寸時大小一樣,也可根據(jù)需求自定義比例。
#define IsIphone6P SCREEN_WIDTH==414
#define SizeScale (IsIphone6P ? 1.2 : 1)
#define kFontSize(value) value*SizeScale
#define kFont(value) [UIFont systemFontOfSize:value*SizeScale]
2.2 xib或SB布局-類別
xib或sb中需要設(shè)置字體的也就是UIButton、UILabel、UITextView、UITextField這幾種,通過創(chuàng)建類別實現(xiàn)(Runtime方式的黑魔法method swizzling)
注意:最近有消息稱代碼中含有method_exchangeImplementations函數(shù),上架可能被拒,本人并未親測,如有情況歡迎留言
創(chuàng)建一個類FontCategory:
FontCategory.h:
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
@interface FontCategory : NSObject
@end
/**
* button
*/
@interface UIButton (MyFont)
@end
/**
* Label
*/
@interface UILabel (MyFont)
@end
/**
* TextField
*/
@interface UITextField (MyFont)
@end
/**
* TextView
*/
@interface UITextView (MyFont)
@end
FontCategory.m:
#import "FontCategory.h"
@implementation FontCategory
@end
@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