10.如何设置全局字体
来源:互联网 发布:淘宝店铺过户流程图 编辑:程序博客网 时间:2024/06/03 11:03
有时候为了统一界面中所有的 Label,Button ,UITextField 等的字体,我们在初始化的时候就需要不断地添加冗余的代码来设置自己的字体。
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
关联文章地址:
Objective-C Runtime 运行时之四:Method Swizzling
CocoaChina 之 Method Swizzling:
0 0
- 10.如何设置全局字体
- iOS 下如何设置全局字体?
- iOS 下如何设置全局字体?
- 设置Swing全局字体
- Qt---设置全局字体
- Qt 设置全局字体
- 设置Swing的全局字体
- Java应用程序设置全局字体
- ios app全局字体设置
- Android app全局字体设置
- WPF设置全局字体和字体嵌入
- [原创]Swing技巧.7:设置全局字体
- 【转】通过UIManager设置全局字体
- Android 全局设置应用字体样式
- Android开发之设置APP全局字体
- android 开发 app全局字体的设置
- Android之应用设置全局字体(第三方字体)
- 如何设置字体
- Android View中getViewTreeObserver().addOnGlobalLayoutListener()
- mysql 1129 error
- Android基础篇(三)——Android中的Activity简单介绍
- Java线程
- 简单冒泡排序
- 10.如何设置全局字体
- 关于Xcode遇到崩溃问题那些事(一)
- DirectShow学习笔记总结
- 第二发~次方求模~
- BigData学习1_数据处理平台架构中的SMACK组合:Spark、Mesos、Akka、Cassandra以及Kafka
- WebServlet注解
- 【solr 4.7.2】windows下安装部署到tomcat6中
- ResumeThread用于一个已结束的线程
- window Eclipse中运行cygwin Hadoop!