美文网首页iOS
UIFont字体适配

UIFont字体适配

作者: 飞鱼ll | 来源:发表于2018-03-13 14:21 被阅读1次

代码适配

#import "UIFont+DXFont.h"
#import <objc/runtime.h>

#define MyUIScreen  375 // UI设计原型图的手机尺寸宽度(6s), 6p的--414

@implementation UIFont (DXFont)

+ (void)load{
    
    //获取替换后的类方法
    Method newMethod = class_getClassMethod([self class], @selector(adjustFont:));
    //获取替换前的类方法
    Method method = class_getClassMethod([self class], @selector(systemFontOfSize:));
    //然后交换类方法
    method_exchangeImplementations(newMethod, method);
    
}

// 以6s未基准(因为一般UI设计是以6s尺寸为基准设计的)的字体。在5s和6P上会根据屏幕尺寸,字体大小会相应的扩大和缩小
+ (UIFont *)adjustFont:(CGFloat)fontSize {
    UIFont *newFont = nil;
    newFont = [UIFont adjustFont:fontSize * [UIScreen mainScreen].bounds.size.width/MyUIScreen];
    return newFont;
}

@end

xib适配

#import "UILabel+DXFont.h"
#import <objc/runtime.h>

@implementation UILabel (DXFont)

+ (void)load{
    //利用running time运行池的方法在程序启动的时候把两个方法替换 适用Xib建立的label
    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) {
        CGFloat fontSize = self.font.pointSize;
        self.font = [UIFont systemFontOfSize:fontSize];
    }
    return self;
}

@end
#import "UIButton+DXFont.h"
#import <objc/runtime.h>

@implementation UIButton (DXFont)

+ (void)load{
    
    //利用running time运行池的方法在程序启动的时候把两个方法替换 适用Xib建立的label
    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) {
        CGFloat fontSize = self.titleLabel.font.pointSize;
        self.titleLabel.font = [UIFont systemFontOfSize:fontSize];
    }
    return self;
}

@end

参考文章:
https://www.jianshu.com/p/8b8731ed84f8
https://www.jianshu.com/p/0541caf7cecb

相关文章

  • 全局配置UIFont

    全局修改UIFont,根据屏幕适配字体大小

  • UIFont字体适配

    代码适配 xib适配 参考文章:https://www.jianshu.com/p/8b8731ed84f8htt...

  • UIFont - iOS字体适配

    首先应该写个runtime 控制下纯代码的字体 然后 nib字体控制 各种控件都写一遍吧 收工 偶尔myInitW...

  • iOS开发 平方字体版本适配错误

    平方字体在iOS9.0之后才有,如需通过字体名字使用需要注意适配 iOS9.0之前版本,否则会无法生成UIFont...

  • IOS UIFont 字体大全

    IOS UIFont 字体大全

  • 无标题文章

    //需要判断是否登录 NSString *str = @"866.44"; //设置字体UIFont UIFont...

  • UIFont

    UIFont(字体) 父类是NSObject UIFont常用方法 系统默认字体(参数是多少号字)label.fo...

  • iOS-获取UIFont 相关默认属性

    目录 一 获取 UIFont 所有字体 打印结果 二 获取 UIFont 系统默认字体 当我们创建一个 UILab...

  • 字体 uifont

    UIFont 设置字体 - 路不平 - 博客频道 - CSDN.NET

  • UIFont 字体

    label.font = [UIFont fontWithName:@"Arial-BoldItalicMT" s...

网友评论

    本文标题:UIFont字体适配

    本文链接:https://www.haomeiwen.com/subject/xvdzfftx.html