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 <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值设置成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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 11,861评论 0 17
  • 一.iPhone X尺寸问题 1. 高度增加了145pt,变成812pt. 2.屏幕圆角显示,注意至少留10pt边...
    骑行天下阅读 14,429评论 5 36
  • 因为要结局swift3.0中引用snapKit的问题,看到一篇介绍Xcode8,swift3变化的文章,觉得很详细...
    uniapp阅读 10,091评论 0 12
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,079评论 25 709
  • 当你翻开故事书, 里面会绽放出一种奇特的魔法。 它是五颜六色的,像漩涡一样, 你进去就会来到一个童话的森林。 那里...
    抽风一刻阅读 1,810评论 0 2

友情链接更多精彩内容