ios 故事板中的自定义字体?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9090745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Custom font in a storyboard?
提问by slycrel
I have a font added to my iOS project. It's in the project and copied when the project builds. It appears to be a valid font and will show up if I list all fonts on the device through the app. I have the plist set up properly to include the font. I can't seem to get the find to show up to use it in my text controls, buttons, etc in Xcode 4.2's storyboard section. Further it doesn't appear to allow me to type the font name, it forces me to use the font dialog. I attempted to install this font in the system as well, but cannot seem to get it to show in this list. Do I need to do this in code only or can I do this through the storyboard interface?
我的 iOS 项目中添加了一种字体。它在项目中并在项目构建时复制。它似乎是一种有效的字体,如果我通过应用程序列出设备上的所有字体,它就会显示出来。我已正确设置 plist 以包含字体。我似乎无法在 Xcode 4.2 的故事板部分的文本控件、按钮等中显示找到以使用它。此外,它似乎不允许我输入字体名称,它迫使我使用字体对话框。我也尝试在系统中安装此字体,但似乎无法将其显示在此列表中。我只需要在代码中执行此操作还是可以通过故事板界面执行此操作?
Note that I can get this working in code, but it would be much more convenient to do this via the storyboard.
请注意,我可以在代码中实现这一点,但通过情节提要来做到这一点会方便得多。
回答by redent84
UPDATE: Xcode 6 Interface Builder now allows you to select custom fonts and will render them at design time correctly.
更新:Xcode 6 Interface Builder 现在允许您选择自定义字体并在设计时正确渲染它们。
I know this question is quite old, but I've been struggling to find an easy way to specify a custom font in Storyboard (or Interface Builder) easily for iOS 5 and I found a quite convenient solution.
我知道这个问题已经很老了,但我一直在努力寻找一种简单的方法来轻松地为 iOS 5 在 Storyboard(或 Interface Builder)中指定自定义字体,我找到了一个非常方便的解决方案。
First, make sure that you've added the font to the project by following this tutorial. Also remember that UINavigationBar, UITabBar and UISegmentedControl custom font can be specified by using the setTitleTextAttributes:
method of the UIAppearance proxy.
首先,确保您已按照本教程将字体添加到项目中。还要记住,可以使用UIAppearance 代理的setTitleTextAttributes:
方法指定 UINavigationBar、UITabBar 和 UISegmentedControl 自定义字体。
Add the categories below to the project for UIButton, UITextField, UILabel and any other component that needs a custom font. The categories simply implement a new property fontName
which changes the current font of the element while maintaining the font size.
将以下类别添加到 UIButton、UITextField、UILabel 和任何其他需要自定义字体的组件的项目中。类别只是实现了一个新属性fontName
,该属性在保持字体大小的同时更改元素的当前字体。
To specify the font in Storyboard, just select the desired element (label, button, textview, etc) and add a User Defined Runtime Attributewith Key Pathset to fontNameof type Stringand value with the name of your custom font.
要在 Storyboard 中指定字体,只需选择所需的元素(标签、按钮、文本视图等)并添加一个用户定义的运行时属性,其中键路径设置为String类型的fontName和带有自定义字体名称的值。
And that's it, you don't even need to import the categories. This way you don't need an outlet for every UI component that requires a custom font and you don't need to code it manually.
就是这样,您甚至不需要导入类别。这样您就不需要为每个需要自定义字体的 UI 组件设置一个插座,也不需要手动对其进行编码。
Take into account that the font won't show in Storyboard, but you will see it when running on your device or simulator.
考虑到字体不会显示在 Storyboard 中,但在您的设备或模拟器上运行时您会看到它。
Category files
类别文件
UIButton+TCCustomFont.h:
UIButton+TCCustomFont.h:
#import <UIKit/UIKit.h>
@interface UIButton (TCCustomFont)
@property (nonatomic, copy) NSString* fontName;
@end
UIButton+TCCustomFont.m:
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:
UILabel+TCCustomFont.h:
#import <UIKit/UIKit.h>
@interface UILabel (TCCustomFont)
@property (nonatomic, copy) NSString* fontName;
@end
UILabel+TCCustomFont.m:
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:
UITextField+TCCustomFont.h:
#import <UIKit/UIKit.h>
@interface UITextField (TCCustomFont)
@property (nonatomic, copy) NSString* fontName;
@end
UITextField+TCCustomFont.m:
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
Also downloadable from GISTand also as a single file.
Troubleshooting
故障排除
If you run against runtime errors because the fontName
property is not specified just add the flag -all_load
under Other linker Flagsin project settings to force the linker to include the categories.
如果由于fontName
未指定属性而导致运行时出现错误,只需在项目设置中的其他链接器标志-all_load
下添加标志以强制链接器包含类别。
回答by monjer
From Xcode6.0 , as Xcode6.0 releasenote:
从 Xcode6.0 开始,作为Xcode6.0 发行说明:
Interface Builder renders embedded custom iOS fonts during design time, giving a more accurate preview of how the finished app will look, with correct dimensions.
Interface Builder 在设计时渲染嵌入的自定义 iOS 字体,以更准确的尺寸预览完成的应用程序的外观。
you can set label font in storyboard.You can do as follow
您可以在故事板中设置标签字体。您可以执行以下操作
- Get you custom font file(.ttf,.ttc)
Import the font files to your Xcode project
In the app-info.plist,add a key named Fonts provided by application.It's an array type , add all your font file name to the array,note:including the file extension.
- 获取自定义字体文件(.ttf、.ttc)
将字体文件导入您的 Xcode 项目
在app-info.plist 中,添加一个名为Fonts 由应用程序提供的键。它是一个数组类型,将所有字体文件名添加到数组中,注意:包括文件扩展名。
- In the storyboard , drag a UILabel to you interface,select the label , and navigate to the Attribute Inspector,click the right icon button of the Font select area.In the popup panel , choose Fontto Custom, and choose the Familyof you embeded font name.
- 在storyboard中,拖一个UILabel到你的界面,选择label,导航到Attribute Inspector,点击Font select区域右边的图标按钮。在弹出的面板中,选择Fontto Custom,然后选择你嵌入的Family字体名称。
ref to
参考
回答by Dan
This is a bug in XCode, been there since 3.x and still not fixed. I too have faced the same issue, even tried adding it to my system with no luck. Here is another SO post about it Fonts not displaying in Interface Builder
这是 XCode 中的一个错误,自 3.x 以来就存在,但仍未修复。我也遇到了同样的问题,甚至尝试将它添加到我的系统中,但没有成功。这是另一篇关于它的 SO 帖子字体未显示在界面生成器中
回答by HugglesNL
Monjer's answer is the correct one. If you, like me, don't see the added font after compiling, make sure to add the font's to the build phases (targets -> build phases -> copy bundle resources -> add your font files)
Monjer 的答案是正确的。如果您和我一样,在编译后没有看到添加的字体,请确保将字体添加到构建阶段(目标 -> 构建阶段 -> 复制捆绑资源 -> 添加字体文件)
回答by Nirav Bhatt
As of XCode 9.4, XCode still does not respect fonts directly in the storyboard. You can see them at design time, but not at runtime. If you are using fontWithName API, you get to know it returns nil, but when used in storyboard/xib, there is no way to know why it doesn't appear.
从 XCode 9.4 开始,XCode 仍然不直接在情节提要中尊重字体。您可以在设计时看到它们,但不能在运行时看到它们。如果你使用 fontWithName API,你会知道它返回 nil,但是当在 storyboard/xib 中使用时,没有办法知道它为什么不出现。
The only way to make sure it works readily from Storyboard/XIB at runtime is to add .ttc/.ttf into
Copy Bundle Resources
Build Phase.As of 9.4, adding font file names to info.plist seems no longer a requirement.
确保它在运行时从 Storyboard/XIB 轻松工作的唯一方法是将 .ttc/.ttf 添加到
Copy Bundle Resources
构建阶段。从 9.4 开始,似乎不再需要向 info.plist 添加字体文件名。
回答by Jules
None of these worked for me, but this did.
这些都不适合我,但确实如此。
#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
回答by Rodrigo Ruiz
redent84's solution was awesome!
redent84 的解决方案很棒!
I'd just add an improvement, add the category on NSObject, so you only have to do it once.
我只是添加一个改进,在 NSObject 上添加类别,所以你只需要做一次。
NSObject+CustomFont.h
NSObject+自定义字体.h
#import <Foundation/Foundation.h>
@interface NSObject (CustomFont)
@property (strong, nonatomic) NSString *fontName;
@property (strong, nonatomic) UIFont *font;
@end
NSObject+CustomFont.m
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