iOS开发之字号适配实现

一个iOS开发项目无外乎就是纯代码布局、xib或SB布局。那么如何实现两个方式的字号适配呢?

字号适配------纯代码

定义一个宏定义如下:

#define SizeScale (SCREEN_WIDTH != 414 ?1 :1.2)

#define kFont(value) [UIFont systemFontOfSize:value * SizeScale]

宏中的1.2是在plus下的大小放大比例。

纯代码中设置字号通过使用这个宏来实现整体适配

字号适配------xib或SB

字号适配无外乎就是设置UIButton、UILabel、UITextView、UITextField的字号
通过创建这几个的类目来实现(Runtime方式的黑魔法method swizzling)
废话不多说,直接上代码:
.h

#import <UIKit/UIKit.h>

#import <objc/runtime.h>



/**

 *  button

 */

@interface UIButton (MyFont)


@end



/**

 *  Label

 */

@interface UILabel (myFont)


@end


/**

 *  TextField

 */


@interface UITextField (myFont)


@end


/**

 *  TextView

 */

@interface UITextView (myFont)


@end


.m


#import "UIButton+MyFont.h"


//不同设备的屏幕比例(当然倍数可以自己控制)


@implementation UIButton (MyFont)


+ (void)load{

    Method imp = class_getInstanceMethod([selfclass], @selector(initWithCoder:));

    Method myImp = class_getInstanceMethod([selfclass], @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 = [UIFontsystemFontOfSize:fontSize * SizeScale];

        }

    }

    return self;

}


@end



@implementation UILabel (myFont)


+ (void)load{

    Method imp = class_getInstanceMethod([selfclass], @selector(initWithCoder:));

    Method myImp = class_getInstanceMethod([selfclass], @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 = [UIFontsystemFontOfSize:fontSize * SizeScale];

        }

    }

    return self;

}


@end


@implementation UITextField (myFont)


+ (void)load{

    Method imp = class_getInstanceMethod([selfclass], @selector(initWithCoder:));

    Method myImp = class_getInstanceMethod([selfclass], @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 = [UIFontsystemFontOfSize:fontSize * SizeScale];

        }

    }

    return self;

}


@end


@implementation UITextView (myFont)


+ (void)load{

    Method imp = class_getInstanceMethod([selfclass], @selector(initWithCoder:));

    Method myImp = class_getInstanceMethod([selfclass], @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 = [UIFontsystemFontOfSize:fontSize * SizeScale];

        }

    }

    return self;

}


@end


  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sailip

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值