iOS开发小技巧 - runtime适配字体
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适配字体的更多相关文章
- iOS开发小技巧 - UILabel添加中划线
iOS开发小技巧 遇到的问题: 给Label添加中划线,然后并没有效果 NSString *str = [NSString stringWithFormat:@"合计金额 ¥%.2f&quo ...
- iOS开发小技巧 -- tableView-section圆角边框解决方案
[iOS开发]tableView-section圆角边框解决方案 tableView圆角边框解决方案 iOS 7之前,图下圆角边框很容易设置 iOS 7之后,tableviewcell的风格不再是圆角 ...
- iOS开发小技巧--即时通讯项目:消息发送框(UITextView)高度的变化; 以及UITextView光标复位的小技巧
1.即时通讯项目中输入框(UITextView)跟随输入文字的增多,高度变化的实现 最主要的方法就是监听UITextView的文字变化的方法- (void)textViewDidChange:(UIT ...
- ios开发小技巧之提示音播放与震动
在ios开发中,有时候我们需要频繁播放某种提示声音,比如微博刷新提示音.QQ消息提示音等,对于这些短小且需要频繁播放的音频,最好将其加入到系统声音(system sound)里. 注意: 需要播放的音 ...
- 【转】IOS开发小技巧
1,Search Bar 怎样去掉背景的颜色(storyboard里只能设置background颜色,可是发现clear Color无法使用). 其实在代码里还是可以设置的,那就是删除背景view [ ...
- ios开发小技巧(转)
1.通过下面方式可以获取图片的像素颜色点:- (void*)getImageData:(UIImage*)image{ void* imageData; if (imageData == ...
- iOS开发小技巧--字典和数组的中文输出
一.在解析json数据的时候,得到的集合对象或者数组对象在用%@打印的时候回出现类似乱码的情况.如图: 在iOS中打印字典或者数组对象,系统会默认调用字典对象和数组对象的descriptionWith ...
- IOS 开发小技巧总结
一.添加自定义字体 1.把字体文件拖到工程中. 2.plist 文件中添加字段:<Array>Fonts provided by application</Array> 把字体 ...
- iOS开发小技巧--相机相册的正确打开方式
iOS相机相册的正确打开方式- UIImagePickerController 通过指定sourceType来实现打开相册还是相机 UIImagePickerControllerSourceTypeP ...
随机推荐
- 类里的通用成员函数应声明为static
类C的成员函数f,如果f的实现实现不依赖于C的任何成员变量,则f为通用函数. 对于通用函数f,可以将其从类C中分离出来做成一个全局函数,也可以仍然让它属于类C,但加上static. 两种处理方法实际都 ...
- 利用Regsvr32绕过Applocker的限制策略
Metasploit Metasploit框架有一个特定的有效载荷,可用于通过Regsvr32实用程序实现自动化绕过AppLocker exploit/windows/misc/regsvr32_ap ...
- C++/C课程设计(2)工资管理系统功能说明
原文取自个人博客:www.jycoder.com欢迎訪问 百度网盘下载源码:Demo.zip 百度网盘下载软件文档:软件文档.zip 工资管理系统 一, 基本功能要求: 1)以password ...
- 令新手头疼的modelsim库编译
估计很多人买了CB哥的书来看吧,他们在学习modelsim仿真的过程中可能遇到过明明是按照书上的步骤添加器件库的了,但还是出现如下的错误: 首先,我想说的是CB哥书上的modelsim-altera1 ...
- 解决code::blocks 17.12不能debug的方法
错误提示: You need to specify a debugger program in the debuggers's settings. 解决方法: 在settings->debugg ...
- Egret入门了解
0.前言 这个星期没有什么事做,就想找点技术了解一下.前段时间看过Egret,用来开发HTML5小游戏.一开始以为很麻烦的,但是经过这两天了解了一下,如果用这个游戏引擎来开发一些简单的游戏,还是蛮方便 ...
- 编写一个C语言函数,要求输入一个url,输出该url是首页、目录页或者其他url
编写一个C语言函数,要求输入一个url,输出该url是首页.目录页或者其他url 首页.目录页或者其他url 如下形式叫做首页: militia.info/ www.apcnc.com.cn/ htt ...
- mysql中delimiter
delimiter xx 其实就是告诉MySQL解释器,该段命令已经结束了,mysql是否可以执行了.默认请况下delimiter是分号';'.
- C++类中的成员函数和构造函数为模板函数时的调用方法
所谓模板函数其实就是建立一个通用函数,这个通用函数的形参类型不具体指定,用一个虚拟类型来代表,这个通用函数就被称为函数模板. 例: #include <iostream> using na ...
- lua -- encode and decode
json.encode 将表格数据编码为 JSON 字符串. 格式: jsonString = json.encode(表格对象) 用法示例: ,b=,c2=},d={,},}) echo(str) ...