有什么方法可以将全局字体 [新自定义字体] 应用于 iphone Objective-c 中的整个应用程序。
我知道我们可以使用下面的方法为每个标签设置字体
[self.titleLabel setFont:[UIFont fontWithName:@"FONOT_NAME" size:FONT_SIZE]];
但我想改变整个应用程序。如果有人知道,请帮助我。
有什么方法可以将全局字体 [新自定义字体] 应用于 iphone Objective-c 中的整个应用程序。
我知道我们可以使用下面的方法为每个标签设置字体
[self.titleLabel setFont:[UIFont fontWithName:@"FONOT_NAME" size:FONT_SIZE]];
但我想改变整个应用程序。如果有人知道,请帮助我。
显然,要完全更改所有 UILabel,您需要在 UILabel 上设置一个类别并更改默认字体。所以这里有一个解决方案:
创建文件 CustomFontLabel.h
@interface UILabel(changeFont)
- (void)awakeFromNib;
-(id)initWithFrame:(CGRect)frame;
@end
创建文件 CustomFontLabel.m
@implementation UILabel(changeFont)
- (void)awakeFromNib
{
[super awakeFromNib];
[self setFont:[UIFont fontWithName:@"Zapfino" size:12.0]];
}
-(id)initWithFrame:(CGRect)frame
{
id result = [super initWithFrame:frame];
if (result) {
[self setFont:[UIFont fontWithName:@"Zapfino" size:12.0]];
}
return result;
}
@end
现在...在您想要这些自定义字体标签的任何视图控制器中,只需在顶部包含:
#import "CustomFontLabel.h"
就是这样——祝你好运
Ican 的类别解决方案可能只是为了节省时间。但是避免使用类别来覆盖现有方法,苹果解释说: 避免类别方法名称冲突
...如果在一个类别中声明的方法的名称与原始类中的方法相同,或者同一类(甚至超类)上的另一个类别中的方法相同,则行为未定义至哪个方法实现在运行时使用。...
另请注意,覆盖-(id) init;
比覆盖更安全-(id)initWithFrame:(CGRect)frame
。单击 UIButtons 上的标签时,您不会遇到无法接收触摸事件的问题。
你是这个意思吗?
@interface GlobalMethods
+(UIFont *)appFont;
@end
@implementation GlobalMethods
+(UIFont *)appFont{
return [UIFont fontWithName:@"someFontName" size:someFontSize];
}
@end
...
[self.titleLabel setFont:[GlobalMethods appFont]];
如果您想以某种方式自动执行此操作(而不setFont
在每个控件上使用),我认为这是不可能的。
如果您可以将您的应用程序(或此特定功能)限制为 iOS 5,那么将会有一个新的 API 可以让您非常方便地为默认 UI 设置外观。我不能给你详细信息,因为在我写这篇文章的时候它们仍然处于保密协议之下。查看 iOS 5 beta SDK 以了解更多信息。
自定义标签.h
#import <UIKit/UIKit.h>
@interface VVLabel : UILabel
@end
自定义标签.m
#import "CustomLabel.h"
#define FontDefaultName @"YourFontName"
@implementation VVLabel
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder: aDecoder];
if (self) {
// Initialization code
// Static font size
self.font = [UIFont fontWithName:FontDefaultName size:17];
// If you want dynamic font size (Get font size from storyboard / From XIB then put below line)
self.font = [UIFont fontWithName:FontDefaultName size:self.font.pointSize];
}
return self;
}