iOS 下如何设置全局字体?
2016-03-04 15:11
447 查看
背景
iOS 6跟
iOS 7的字体还是有点不一样的,有时候为了两者的统一,或者,应设计师的要求,界面中所有的Label,Button 等都用自定义字体,一般来说,我们在初始化的时候就需要不断地添加冗余的代码来设置自己的字体。
UILabel *label = [[UILabel alloc] init]; label.font = [UIFont fontWithName:@"myFont"]; ...如果你的界面全部是代码实现的,而且项目初期就已经定下统一用什么字体了,这就不是什么难事。但是,试想,如果你的界面是由大量
IB实现的,而且用的是自定义的字体,在
IB中选都没法选;或是项目已经完成差不多了,上面要求统一改字体,那该如何是好?其实利用
objective-c的动态性就可以轻松搞定。
Method swizzling
什么是Method Swizzling请
Xib加载的界面的UIButton, UILabel的字体,其他的如UITextField等类似,新建Catogery就好,想修改代码生成的界面,修改 initWithCoder 为 init就好
#import <UIKit/UIKit.h> #import <objc/runtime.h> @interface UIButton (myFont) @end @interface UILabel (myFont) @end @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) { CGFloat fontSize = self.titleLabel.font.pointSize; self.titleLabel.font = <# Your Font Here #>; } 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) { CGFloat fontSize = self.font.pointSize; self.font = <# Your Font Here #>; } return self; } @end
相关文章推荐
- 【iOS】BSXPCMessage received error for message: Connection interrupted的解决办法
- iOS 删除已经配置的类库和移除CocoaPods
- iOS 中KVC、KVO、NSNotification、delegate 总结及区别
- iOS中 第三方LBXScan库二维码扫描
- iOS 中的NSTimer定时器的作用
- iOS 引入支付宝 缺少 #include <openssl/asn1.h>
- iOS ,不提交审核,修复某些线上bug以及线上更新
- iOS开发资源汇总
- iOS KVC和一些基础知识了解
- iOS makeKeyAndVisible
- 【iOS】KVC 和 KVO 的使用场景
- iOS动画
- IOS开发之github客户端上传代码 mac 注意事项《转载》
- IOS学习 Notification 用通知传值
- iOS中9.0与8.0两个版本通讯录操作(兼容版)
- iOS dispatch队列
- Block code execution after alert view is displayed in iOS
- iOS之多线程精髓
- ios 热修复
- ios 获取ios系统信息