ios笔记 可视化编程字体大小适配

创建个UIButton类目, 名字叫AutoFontSize;

.h文件代码

#import <UIKit/UIKit.h>

#import <objc/runtime.h>



/**

 *  button

 */


@interface UIButton (AutoFontSize)


@end


/**

 *  Label

 */

@interface UILabel (AutoFontSize)


@end


/**

 *  TextField

 */


@interface UITextField (AutoFontSize)


@end


/**

 *  TextView

 */

@interface UITextView (AutoFontSize)

@end


.m文件代码


#import "UIButton+AutoFontSize.h"


@implementation UIButton (AutoFontSize)


+ (void)load{

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

    Method myImp =class_getInstanceMethod([selfclass], @selector(myInitWithCoder:));

    method_exchangeImplementations(imp, myImp);

}


- (id)myInitWithCoder:(NSCoder*)aDecode{

    [selfmyInitWithCoder:aDecode];

    if (self) {

        

        //部分不像改变字体的tag值设置成333跳过

        if(self.titleLabel.tag != 333){

            CGFloat fontSize =self.titleLabel.font.pointSize;

            self.titleLabel.font = [UIFontsystemFontOfSize:fontSize *OffWidth];

        }

    }

    returnself;

}


@end



@implementation UILabel (AutoFontSize)


+ (void)load{

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

    Method myImp =class_getInstanceMethod([selfclass], @selector(myInitWithCoder:));

    method_exchangeImplementations(imp, myImp);

}


- (id)myInitWithCoder:(NSCoder*)aDecode{

    [selfmyInitWithCoder:aDecode];

    if (self) {

        NSLog(@"%@",self.font);

        //部分不像改变字体的tag值设置成333跳过

        if(self.tag != 333){

            CGFloat fontSize =self.font.pointSize;

            self.font = [UIFontsystemFontOfSize:fontSize *OffWidth - 1];

        }

    }

    returnself;

}


@end


@implementation UITextField (AutoFontSize)


+ (void)load{

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

    Method myImp =class_getInstanceMethod([selfclass], @selector(myInitWithCoder:));

    method_exchangeImplementations(imp, myImp);

}


- (id)myInitWithCoder:(NSCoder*)aDecode{

    [selfmyInitWithCoder:aDecode];

    if (self) {

        //部分不像改变字体的tag值设置成333跳过

        if(self.tag != 333){

            CGFloat fontSize =self.font.pointSize;

            self.font = [UIFontsystemFontOfSize:fontSize *OffWidth];

        }

    }

    returnself;

}


@end


@implementation UITextView (AutoFontSize)


+ (void)load{

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

    Method myImp =class_getInstanceMethod([selfclass], @selector(myInitWithCoder:));

    method_exchangeImplementations(imp, myImp);

}


- (id)myInitWithCoder:(NSCoder*)aDecode{

    [selfmyInitWithCoder:aDecode];

    if (self) {

        //部分不像改变字体的tag值设置成333跳过

        if(self.tag != 333){

            CGFloat fontSize =self.font.pointSize;

            self.font = [UIFontsystemFontOfSize:fontSize *OffWidth];

        }

    }

    returnself;

}


@end


在pch文件里import

#import "UIButton+AutoFontSize.h"


完事
原理:

 中二点的叫rutime黑魔法 :method_exchangeImplementations 交换系统方法, 当系统需要调用initWithCoder方法时使其不去调用initWithCoder而是去调用自己定义的myInitWithCoder方法, 在方法里把字体大小乘上屏幕比例



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值