故事板中的自定义字体? [英] Custom font in a storyboard?

查看:226
本文介绍了故事板中的自定义字体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字体添加到我的iOS项目。它在项目中,并在项目构建时复制。它似乎是一个有效的字体,如果我通过应用程序列出设备上的所有字体,将显示。我有plist设置正确包括字体。我似乎不能得到发现显示使用它在我的文本控件,按钮等在Xcode 4.2的故事板部分。此外,它似乎不允许我键入字体名称,它强迫我使用字体对话框。我试图在系统中安装此字体,但似乎不能让它显示在此列表中。我需要在代码中这么做或者我可以通过故事板界面这样做吗?



请注意,我可以在代码中工作,但它会更多

解决方案

更新:Xcode 6 Interface Builder现在允许您选择自定义字体并渲染它们在设计时正确。



我知道这个问题很老,但我一直在努力找到一个简单的方法来指定一个自定义字体在故事板)。



首先,确保你已经通过以下方式将字体添加到项目中:以及单个文件



疑难解答



未指定 fontName 属性的运行时错误只是添加其他链接器标志下的 -all_load strong>在项目设置中强制链接器包括类别。


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?

Note that I can get this working in code, but it would be much more convenient to do this via the storyboard.

解决方案

UPDATE: Xcode 6 Interface Builder now allows you to select custom fonts and will render them at design time correctly.

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.

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.

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.

To specify the font in Storyboard, just select the desired element (label, button, textview, etc) and add a User Defined Runtime Attribute with Key Path set to fontName of type String and value with the name of your custom font.

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.

Take into account that the font won't show in Storyboard, but you will see it when running on your device or simulator.


Category files

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

Also downloadable from GIST and 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 Flags in project settings to force the linker to include the categories.

这篇关于故事板中的自定义字体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆