1.先建立一个UILabel的分类

导入#import <objc/runtime.h>头文件

2.在.m文件中写入如下代码

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

#define IPHONE_HEIGHT  [UIScreen mainScreen].bounds.size.height

#define SizeScale ((IPHONE_HEIGHT > 568) ? IPHONE_HEIGHT/568 : 1)

@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);

Method cmp = class_getInstanceMethod([self class], @selector(initWithFrame:));

Method myCmp = class_getInstanceMethod([self class], @selector(myInitWithFrame:));

method_exchangeImplementations(cmp, myCmp);

}

- (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];

NSLog(@" label的大小 == %f", self.font.pointSize);

}

}

return self;

}

- (id)myInitWithFrame:(CGRect)frame{

[self myInitWithFrame:frame];

if(self){

CGFloat fontSize = self.font.pointSize;

self.font = [UIFont systemFontOfSize:fontSize * SizeScale];

NSLog(@" label的大小 == %f", self.font.pointSize);

}

return self;

}

@end

3.在调用文件中导入分类

#import "ViewController.h"

#import "UIButton+myFont.h"

@interface ViewController ()

/*注释*/

@property (nonatomic,strong)UILabel *label;

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

[self.view addSubview:self.label];

// Do any additional setup after loading the view, typically from a nib.

}

- (void)viewWillAppear:(BOOL)animated

{

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

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

- (UILabel *)label{

if (!_label) {

_label = [[UILabel alloc]initWithFrame:CGRectMake(50, 100, 100, 30)];

//        _label.font = [UIFont systemFontOfSize:17];

_label.tag = 32;

_label.text = @"这是测试文字";

}

return _label;

}

@end

iOS -不同模拟器字体适配的更多相关文章

  1. iOS 4s-6Plus屏幕自动适配及颜色转换为十六进制

    iOS各种屏幕自动适配及颜色转换为十六进制 ★★★XLJMatchScreen自动适配屏幕★★★ 支持pod导入 pod 'XLJScreenMatching', '~> 1.0.3' 如果发现 ...

  2. iOS 向模拟器里添加照片

    iOS 向模拟器里添加照片 模拟器里Photos最开始时是没有照片的,有时我们做Demo时需要Photos里面的照片做测试,这时就需要把Mac上的照片导入到模拟器里.步骤如下: 1,打开模拟器 2,选 ...

  3. iOS 中的字体预览

    要预览iOS的各种字体的效果,可以访问http://iosfonts.com

  4. iOS完整预装字体清单

    iOS完整预装字体清单:http://iosfonts.com/

  5. Android 字体适配方案

    开发过程中,按照UI设计尺寸做好UI页面,当用户自定义自己的手机字体大小之后UI完全没法看了,这个时候就在想让app字体大小始终一致就好了 下面看一下,出现的问题和解决方案     做个简单的例子,先 ...

  6. iOS Simulator 模拟器 与 Android Emulator 仿真器:为什么叫不同的英文名字?(待补充)

    iOS Simulator 模拟器 与 Android Emulator 仿真器:为什么叫不同的英文名字?(待补充)

  7. 显示iOS所有系统字体

    显示iOS所有系统字体 源码地址: https://github.com/YouXianMing/UI-Component-Collection 效果图: 便于你开发中寻找适合自己的字体, demo中 ...

  8. iOS WKWebview 网页开发适配指南

    iOS WKWebview 网页开发适配指南 微信iOS客户端将于2017年3月1日前逐步升级为WKWebview内核,需要网页开发者提前做好网站的兼容检查和适配.如有问题,可参考文末联系方式,向我们 ...

  9. iOS 多尺寸屏幕适配

    Point Point可以理解为iOS程序员眼中的大小单位.它是iOS操作系统中的抽象的概念. Rendered Pixels可以理解为UI设计师眼中的大小单位. Physical Pixels 设备 ...

随机推荐

  1. Metro UI 界面完全解析 (转载)

    Metro在微软的内部开发名称为“ typography-based design language”(基于排版的设计语言).它最早出现在微软电子百科全书95,此后微软又有许多知名产品使用了Metro ...

  2. .NET MVC4 实训记录之一(引入Unity3.0 Ioc框架)

    一直在做维护项目,没有机会接触完整的架构,于是自学.NET MVC.自今日起,将自学的过程.遇到的问题以及解决方案记录下来. 在WebApp项目中右键,使用NuGet引入Unity3.0.

  3. EF的四种开发模式

    EF提供了四种开发模式,具体如下:(转载)Code First(New DataBase) :在代码中定义类和映射关系并通过model生成数据库,使用迁移技术更新数据库.Code First(Exis ...

  4. C# 6.0 功能预览

    C# 6.0 功能预览 (一) 一.索引的成员和元素初始化 1.1 原始初始化集合 Dictionary 1.2 键值初始化集合 Dictionary 1.3 运算符 $ 初始化集合 Dictiona ...

  5. Extension Objects(扩展对象)

    设计模式之美:Extension Objects(扩展对象)   索引 意图 结构 参与者 适用性 效果 相关模式 实现 实现方式(一):使用示例结构实现 Extension Objects. 实现方 ...

  6. IIS7.5下的httpModules设置

    部署到IIS7.5上httpModule不起作用了,原来是iis7里面的网站应用程序池使用了默认的“集成模式”,所以所有的http请求处理都托管给了 IIS,web.config里面的配置就不起效了. ...

  7. C#函数式程序设计之泛型

    Intellij修改archetype Plugin配置 2014-03-16 09:26 by 破狼, 204 阅读, 0 评论,收藏, 编辑 Maven archetype plugin为我们提供 ...

  8. Linq to XML的练习

    假如有以下XML: <?xml version="1.0" encoding="utf-8" ?> - <export> - <p ...

  9. Stimulsoft.Report.web viewer控件添加按钮

    当你购买了带源码的时候,你可以对源码进行修改以达到自己想要的效果,比较这里讲到的,向viewer控件工具栏添加按钮. 通过源码目录可以看出我们需要修改的有两部分代码 红色方块圈中的部分,[StiWeb ...

  10. [转] iOS ABI Function Call Guide

    source: apple ARMv6 Function Calling Conventions When functions (routines) call other functions (sub ...