博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS 之适配字体的几中方法
阅读量:4289 次
发布时间:2019-05-27

本文共 2903 字,大约阅读时间需要 9 分钟。

/**

字体

*/

#define BASEPingFangMedium   12

#define BASEPingFangRegular   12

#define BASEPingFangBold    12

#define PingFangMedium  [UIFont fontWithName:@"PingFang-SC-Medium" size:14];

#define PingFangRegular  [UIFont fontWithName:@"PingFang-SC-Regular" size:14];

#define PingFangBold  [UIFont fontWithName:@"PingFang-SC-Bold" size:14];

1.======

5s 和 6 的PPI 一致,所以,一般美工要求在 5s 和 6 上,字号一样,在 6 Plus 上,字号要增大两号;

Xib 、Storyboard 中的每个需要字号适配的元素都连线到实现文件中,即创建  IBOutlet 对象,然后在代码中,通过判断设备类型,通过代码,设置具体字号。

 

 

2.========runtime,采用分类改变字体大小

 

#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([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

 

 

 

转载地址:http://bcmgi.baihongyu.com/

你可能感兴趣的文章
使用Eclipse把java文件打包成jar 含有第三方jar库的jar包
查看>>
3种web会话管理的方式
查看>>
SSM(框架)-异常1:面向接口式编程异常
查看>>
Android蓝牙4.0之玩爆智能穿戴、家具(二)
查看>>
使用Condition实现多线程之间调用
查看>>
javaAPI之String
查看>>
JQ 新窗口打开链接并设置参数
查看>>
JQuery实现列表中复选框全选反选功能封装
查看>>
JAVA GC 简单总结
查看>>
JS中常遇到的浏览器兼容问题和解决方法
查看>>
JAVA学习笔记之-servlet知识点
查看>>
apache 配置不同的端口访问不同的站点
查看>>
2017年3月Java9带来的革新!
查看>>
Log4j容器深入探究
查看>>
记glide框架使用中所遇到的问题
查看>>
学习AOP之透过Spring的Ioc理解Advisor
查看>>
Jquery一个简单的注册验证
查看>>
SpringMVC基础_ControllerAdvice
查看>>
Toast还能显示图片你知道么?
查看>>
安卓三状态切换按钮TriStateToggleButton
查看>>