更新:Xcode 6 Interface Builder 现在允许您选择自定义字体并在设计时正确呈现它们。
我知道这个问题已经很老了,但是我一直在努力寻找一种简单的方法来为 iOS 5 轻松地在 Storyboard(或 Interface Builder)中指定自定义字体,并且我找到了一个非常方便的解决方案。
首先,确保您已按照本教程将字体添加到项目中。还要记住 UINavigationBar、UITabBar 和 UISegmentedControl 自定义字体可以通过使用UIAppearance 代理setTitleTextAttributes:
的方法来指定。
将以下类别添加到 UIButton、UITextField、UILabel 和任何其他需要自定义字体的组件的项目中。类别只是实现了一个新属性,该属性fontName
在保持字体大小的同时更改元素的当前字体。
要在 Storyboard 中指定字体,只需选择所需的元素(标签、按钮、文本视图等)并添加一个用户定义的运行时属性,其中键路径设置为字符串类型的fontName ,值与您的自定义字体的名称。

就是这样,您甚至不需要导入类别。这样,您不需要为每个需要自定义字体的 UI 组件提供一个插座,也不需要手动编写代码。
考虑到字体不会在 Storyboard 中显示,但在您的设备或模拟器上运行时您会看到它。
类别文件
UIButton+TCCustomFont.h:
#import <UIKit/UIKit.h>
@interface UIButton (TCCustomFont)
@property (nonatomic, copy) NSString* fontName;
@end
UIButton+TCCustomFont.m:
#import "UIButton+TCCustomFont.h"
@implementation UIButton (TCCustomFont)
- (NSString *)fontName {
return self.titleLabel.font.fontName;
}
- (void)setFontName:(NSString *)fontName {
self.titleLabel.font = [UIFont fontWithName:fontName size:self.titleLabel.font.pointSize];
}
@end
UILabel+TCCustomFont.h:
#import <UIKit/UIKit.h>
@interface UILabel (TCCustomFont)
@property (nonatomic, copy) NSString* fontName;
@end
UILabel+TCCustomFont.m :
#import "UILabel+TCCustomFont.h"
@implementation UILabel (TCCustomFont)
- (NSString *)fontName {
return self.font.fontName;
}
- (void)setFontName:(NSString *)fontName {
self.font = [UIFont fontWithName:fontName size:self.font.pointSize];
}
@end
UITextField+TCCustomFont.h:
#import <UIKit/UIKit.h>
@interface UITextField (TCCustomFont)
@property (nonatomic, copy) NSString* fontName;
@end
UITextField+TCCustomFont.m:
#import "UITextField+TCCustomFont.h"
@implementation UITextField (TCCustomFont)
- (NSString *)fontName {
return self.font.fontName;
}
- (void)setFontName:(NSString *)fontName {
self.font = [UIFont fontWithName:fontName size:self.font.pointSize];
}
@end
也可从 GIST下载,也可作为单个文件下载。
故障排除
如果由于fontName
未指定属性而遇到运行时错误,只需在项目设置中的其他链接器标志-all_load
下添加标志以强制链接器包含类别。