如何为整个应用程序设置自定义字体?

有什么办法来如何应用全局字体[新的自定义字体]在iphone objective-c整个应用程序。

我知道我们可以使用下面的方法为每个标签设置字体

[self.titleLabel setFont:[UIFont fontWithName:@"FONOT_NAME" size:FONT_SIZE]]; 

但我想改变整个应用程序。 如果有人知道,请帮助我。

显然要全部更改所有UILabel,您将需要在UILabel上设置一个类别并更改默认字体。 所以这里有一个解决scheme:

创build一个文件CustomFontLabel.h

 @interface UILabel(changeFont) - (void)awakeFromNib; -(id)initWithFrame:(CGRect)frame; @end 

创build一个文件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的类别解决scheme可能只是为了节省一天的时间。 但是,避免使用类别覆盖现有的方法,苹果解释: 避免类别方法名称冲突

…如果在类别中声明的方法的名称与原始类中的方法相同,或者在同一个类(甚至是超类)上的另一个类别中的方法相同,则行为是未定义的在运行时使用。 …

还要注意覆盖-(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每个控件),我不相信这是可能的。

如果您可以将应用程序(或此特定function)限制到iOS 5,则会有一个新的API来让您非常方便地使用默认UI。 我不能给你详细的资料,因为在我写这个的时候,他们还在NDA之下。 看看iOS 5 beta SDK来了解更多信息。