iOS开发之字体大小适配实现

来源:互联网 发布:8选1数据选择器74ls151 编辑:程序博客网 时间:2024/05/16 04:38

一个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 0
原创粉丝点击