67

我的 iOS 项目中添加了一种字体。它在项目中,并在项目构建时复制。它似乎是一种有效的字体,如果我通过应用程序列出设备上的所有字体,它就会显示出来。我已正确设置 plist 以包含字体。我似乎无法在 Xcode 4.2 的故事板部分中的文本控件、按钮等中使用它。此外,它似乎不允许我输入字体名称,它迫使我使用字体对话框。我也尝试在系统中安装此字体,但似乎无法让它显示在此列表中。我只需要在代码中执行此操作还是可以通过情节提要界面执行此操作?

请注意,我可以在代码中使用它,但通过情节提要执行此操作会更方便。

4

7 回答 7

147

更新: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下添加标志以强制链接器包含类别。

于 2013-03-01T10:06:28.833 回答
60

从 Xcode6.0 开始,作为Xcode6.0 发行说明:

Interface Builder 在设计时渲染嵌入的自定义 iOS 字体,以更准确的尺寸预览最终应用的外观。

您可以在情节提要中设置标签字体。您可以执行以下操作

  1. 获取自定义字体文件(.ttf、 .ttc)

  2. 将字体文件导入您的 Xcode 项目

    在此处输入图像描述

  3. app-info.plist中,添加应用程序提供的名为Fonts的键。它是一个数组类型,将你所有的字体文件名添加到数组中,注意:包括文件扩展名。

在此处输入图像描述

  1. 在 storyboard 中,拖一个 UILabel 到你的界面,选择标签,然后导航到Attribute Inspector,点击Font 选择区域的右图标按钮。在弹出的面板中,选择Font to Custom,然后选择你嵌入的Family字体名称。

在此处输入图像描述

参考

于 2014-12-01T12:15:06.010 回答
5

这是 XCode 中的一个错误,自 3.x 以来一直存在,但仍未修复。我也遇到过同样的问题,甚至尝试将其添加到我的系统中,但没有成功。这是另一篇关于它的 SO 帖子Fonts not displayed in Interface Builder

于 2012-02-01T04:41:19.650 回答
3

Monjer 的答案是正确的。如果你和我一样,在编译后看不到添加的字体,请确保将字体添加到构建阶段(目标 -> 构建阶段 -> 复制捆绑资源 -> 添加字体文件)

于 2015-06-24T13:51:58.517 回答
1

从 XCode 9.4 开始,XCode 仍然不直接在情节提要中尊重字体。您可以在设计时看到它们,但在运行时看不到它们。如果您使用 fontWithName API,您会知道它返回 nil,但在 storyboard/xib 中使用时,无法知道它为什么不出现。

  • 确保它在运行时从 Storyboard/XIB 轻松运行的唯一方法是将 .ttc/.ttf 添加到Copy Bundle Resources构建阶段。

  • 从 9.4 开始,似乎不再需要向 info.plist 添加字体文件名。

于 2018-07-09T13:47:45.660 回答
0

redent84 的解决方案很棒!

我只是添加一个改进,在 NSObject 上添加类别,所以你只需要做一次。

NSObject+CustomFont.h

#import <Foundation/Foundation.h>

@interface NSObject (CustomFont)

@property (strong, nonatomic) NSString *fontName;
@property (strong, nonatomic) UIFont *font;

@end

NSObject+CustomFont.m

#import "NSObject+CustomFont.h"

@implementation NSObject (CustomFont)

@dynamic font;

- (NSString *)fontName
{
    return self.font.fontName;
}

- (void)setFontName:(NSString *)fontName
{
    self.font = [UIFont fontWithName:fontName size:self.font.pointSize];
}

@end
于 2014-08-06T17:06:54.107 回答
0

这些都不适合我,但确实如此。

#import <UIKit/UIKit.h>

@interface UILabelEx : UILabel


@end

#import "UILabelEx.h"
#import "Constants.h"

@implementation UILabelEx

- (void) traitCollectionDidChange: (UITraitCollection *) previousTraitCollection {
    [super traitCollectionDidChange: previousTraitCollection];

    self.font = [UIFont fontWithName:APP_FONT size:self.font.pointSize];   
}
@end
于 2015-11-13T09:12:29.957 回答