iOS 控件及字体大小适配

开发中所涉及的屏幕适配无非2种,控件大小的适配和字体大小的适配。

1. 控件大小的适配

1.1 竖屏下的宏,用来设置控件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 横屏下的宏,用来设置控件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. 字体大小的适配

开发项目2种方式布局:纯代码,xib和storyBoard,所以2种适配方式。

2.1 纯代码布局-宏定义

1.2代表6P尺寸的时候字体为1.2倍,5S和6尺寸时大小一样,也可根据需求自定义比例。

#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中需要设置字体的也就是UIButton、UILabel、UITextView、UITextField这几种,通过创建类别实现(Runtime方式的黑魔法method swizzling)

注意:最近有消息称代码中含有method_exchangeImplementations函数,上架可能被拒,本人并未亲测,如有情况欢迎留言

创建一个类FontCategory:

FontCategory.h:
#import 
#import 

@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值设置成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值设置成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值设置成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值设置成333跳过
        if(self.tag != 333){
            CGFloat fontSize = self.font.pointSize;
            self.font = [UIFont systemFontOfSize:fontSize * SizeScale];
        }
    }
    return self;
}
@end

你可能感兴趣的:(iOS 控件及字体大小适配)