sly*_*rel 63 iphone cocoa-touch storyboard custom-font ios
我的iOS项目中添加了一个字体.它在项目中并在项目构建时复制.它似乎是一个有效的字体,如果我通过应用程序列出设备上的所有字体,它将显示出来.我正确设置了plist以包含字体.我似乎无法在Xcode 4.2的故事板部分的文本控件,按钮等中显示使用它.此外它似乎不允许我键入字体名称,它强制我使用字体对话框.我也尝试在系统中安装此字体,但似乎无法在此列表中显示它.我是否只需要在代码中执行此操作,还是可以通过故事板界面执行此操作?
请注意,我可以在代码中使用它,但通过故事板执行此操作会更方便.
red*_*t84 146
我知道这个问题已经很老了,但我一直在努力找到一种简单的方法来为iOS 5轻松地在Storyboard(或Interface Builder)中指定自定义字体,我找到了一个非常方便的解决方案.
首先,请确保您已按照本教程将字体添加到项目中.还要记住,可以使用UIAppearance代理的setTitleTextAttributes:方法指定UINavigationBar,UITabBar和UISegmentedControl自定义字体.
将以下类别添加到UIButton,UITextField,UILabel以及需要自定义字体的任何其他组件的项目中.这些类别只是实现了一个新属性fontName,它在保持字体大小的同时更改了元素的当前字体.
要在Storyboard中指定字体,只需选择所需的元素(标签,按钮,textview等),然后添加一个用户定义的运行时属性,其键路径设置为String类型的fontName,值和自定义字体的名称.

就是这样,你甚至不需要导入类别.这样,您不需要每个需要自定义字体的UI组件的插座,也不需要手动编码.
考虑到字体不会显示在Storyboard中,但是当您在设备或模拟器上运行时,您会看到它.
UIButton + TCCustomFont.h:
#import <UIKit/UIKit.h>
@interface UIButton (TCCustomFont)
@property (nonatomic, copy) NSString* fontName;
@end
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
UILabel + TCCustomFont.h:
#import <UIKit/UIKit.h>
@interface UILabel (TCCustomFont)
@property (nonatomic, copy) NSString* fontName;
@end
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
UITextField + TCCustomFont.h:
#import <UIKit/UIKit.h>
@interface UITextField (TCCustomFont)
@property (nonatomic, copy) NSString* fontName;
@end
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
如果由于fontName未指定属性而针对运行时错误运行,则只需在项目设置中的其他链接器标志-all_load下添加标志,以强制链接器包含类别.
mon*_*jer 58
从Xcode6.0开始,作为Xcode6.0发布说明:
Interface Builder在设计时呈现嵌入的自定义iOS字体,使用正确的尺寸更准确地预览完成的应用程序的外观.
你可以在storyboard中设置标签字体.你可以这样做
将字体文件导入Xcode项目

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


| 归档时间: |
|
| 查看次数: |
48905 次 |
| 最近记录: |