iOS开发小技巧 - runtime适配字体

版权声明:本文为博主原创文章,未经博主允许不得转载,有问题可联系博主Email: liuyongjiesail@icloud.com

一个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([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

iOS开发小技巧 - runtime适配字体的更多相关文章

  1. iOS开发小技巧 - UILabel添加中划线

    iOS开发小技巧 遇到的问题: 给Label添加中划线,然后并没有效果 NSString *str = [NSString stringWithFormat:@"合计金额 ¥%.2f&quo ...

  2. iOS开发小技巧 -- tableView-section圆角边框解决方案

    [iOS开发]tableView-section圆角边框解决方案 tableView圆角边框解决方案 iOS 7之前,图下圆角边框很容易设置 iOS 7之后,tableviewcell的风格不再是圆角 ...

  3. iOS开发小技巧--即时通讯项目:消息发送框(UITextView)高度的变化; 以及UITextView光标复位的小技巧

    1.即时通讯项目中输入框(UITextView)跟随输入文字的增多,高度变化的实现 最主要的方法就是监听UITextView的文字变化的方法- (void)textViewDidChange:(UIT ...

  4. ios开发小技巧之提示音播放与震动

    在ios开发中,有时候我们需要频繁播放某种提示声音,比如微博刷新提示音.QQ消息提示音等,对于这些短小且需要频繁播放的音频,最好将其加入到系统声音(system sound)里. 注意: 需要播放的音 ...

  5. 【转】IOS开发小技巧

    1,Search Bar 怎样去掉背景的颜色(storyboard里只能设置background颜色,可是发现clear Color无法使用). 其实在代码里还是可以设置的,那就是删除背景view [ ...

  6. ios开发小技巧(转)

    1.通过下面方式可以获取图片的像素颜色点:- (void*)getImageData:(UIImage*)image{    void* imageData;    if (imageData == ...

  7. iOS开发小技巧--字典和数组的中文输出

    一.在解析json数据的时候,得到的集合对象或者数组对象在用%@打印的时候回出现类似乱码的情况.如图: 在iOS中打印字典或者数组对象,系统会默认调用字典对象和数组对象的descriptionWith ...

  8. IOS 开发小技巧总结

    一.添加自定义字体 1.把字体文件拖到工程中. 2.plist 文件中添加字段:<Array>Fonts provided by application</Array> 把字体 ...

  9. iOS开发小技巧--相机相册的正确打开方式

    iOS相机相册的正确打开方式- UIImagePickerController 通过指定sourceType来实现打开相册还是相机 UIImagePickerControllerSourceTypeP ...

随机推荐

  1. Vue基本概念介绍及vue-cli环境搭建

    1 js中初始化一个Vue对象,传的参数就是对象属性. 挂载点.模板.实例之间的关系. var vm = new Vue({ el:"#app", template:'<di ...

  2. 如何在WPF中调用Winform控件

    原文地址:http://hi.baidu.com/stuoopluwqbbeod/item/32ec38403da42ee2bcf45167 功能实现主要分三步:1.添加两个引用:WindowsFor ...

  3. 在eclipse 导入简单的Android studio 简单项目

    前言: 现在Android studio版本已经去到2.0(我暂时用着开发者版本)了,但是还是和以前的版本一样卡. (因为我用了很多第三方的UI控件,导致在Android studio build一个 ...

  4. Python Tkinter基础控件入门实例

    分享一个Python Tkinter基础控件用法的入门例子,包括窗口的显示.显示内置图片.弹出窗口.菜单等. 例子,Python Tkinter基础控件的用法 # -*- coding: utf-8 ...

  5. 行为类模式(四):迭代器(Iterator)

    定义 提供一种方法访问一个容器(container)对象中的各个元素,而又不暴露该对象的内部细节. UML 优点 简化了遍历方式,对于对象集合的遍历,还是比较麻烦的,对于数组或者有序列表,我们尚可以通 ...

  6. BD

    销售圣经:终极销售资源(销售必读,行业经典) 销售圣经2:销售之神的21.5条销售法则 Heart and Sell: 10 Universal Truths Every Salesperson Ne ...

  7. javascript基础拾遗(六)

    1.Date内置对象 获取系统时间 var now = new Date() console.log(now) console.log(now.getDate()) console.log(now.g ...

  8. bash shell(4):读取文件大小,移动文件,复制文件,字符串转数字

    下面是代码:注意: 1.if [ expression ],表达式两边需要空格 2.a=$[aa],字符串转为int类型 3.赋值的时候不能有空格 #!/bin/shell fn="file ...

  9. (转)用stunnel给普通http通信加密

    转自:https://www.digitalocean.com/community/tutorials/how-to-set-up-an-ssl-tunnel-using-stunnel-on-ubu ...

  10. ffmpeg转码参数设置

    ffmpeg用了很久了,也没有想写点什么. 刚接触ffmpeg也是有大量的不理解的地方,不过慢慢的了解多了基本上都是可以使用的. 本文主要介绍如何使用ffmpeg.exe进行转码.编译好的ffmpeg ...