Ios 在interface builder中使用自定义字体

Ios 在interface builder中使用自定义字体,ios,fonts,interface-builder,Ios,Fonts,Interface Builder,我搜索了这个网站,但我发现了一些没有回答的问题 我已将自定义字体加载到我的xcode项目中。A[UIFont-fontWithName:@“Laconic Light”大小:19]有效。但是界面生成器不喜欢这种字体。我不能和IB一起使用,它总是显示默认字体。有没有办法告诉IB可以使用该字体?另一个解决方案是将UILabel子类化以加载到自定义字体中。您可以在IB中引用它,尽管您仍然看不到正确的字体。我在Xcode 4中也有这个问题。在我的程序中,有很多UILabel没有IBOutlets,所以我

我搜索了这个网站,但我发现了一些没有回答的问题


我已将自定义字体加载到我的xcode项目中。A
[UIFont-fontWithName:@“Laconic Light”大小:19]
有效。但是界面生成器不喜欢这种字体。我不能和IB一起使用,它总是显示默认字体。有没有办法告诉IB可以使用该字体?

另一个解决方案是将UILabel子类化以加载到自定义字体中。您可以在IB中引用它,尽管您仍然看不到正确的字体。

我在Xcode 4中也有这个问题。在我的程序中,有很多
UILabel
没有
IBOutlets
,所以我就是这样做的

首先,将
UILabel
子类化为
CustomFontLabel

然后,重写“
awakeFromNib
”方法


最后,在Interface Builder>Identity Inspector中将类更改为
CustomFontLabel

中,我更倾向于以一种更通用的方式执行此操作,它允许您在Interface Builder中调整文本大小,并在运行时简单地替换字体

我为任何UIKit元素创建一个IBCollection属性来设置字体,然后从IB连接适当的项

@property (strong, nonatomic) IBOutletCollection(id) NSArray *lightFontItems;
@property (strong, nonatomic) IBOutletCollection(id) NSArray *regularFontItems;
那么在我看来,我使用了这样的方法:

[self setFontName:@"Roboto-Light" onItemsInArray:[self lightFontItems]];
[self setFontName:@"Roboto-Regular" onItemsInArray:[self regularFontItems]];
+ (void)setFontName:(NSString *)fontName onItemsInArray:(NSArray *)array;
{
  [array each:^(id item) {
    if (![item respondsToSelector:@selector(setFont:)]) return;
    [item performSelector:@selector(setFont:) withObject:[UIFont fontWithName:fontName size:[[item font] pointSize]]];
  }];
}
setlightfontonemsinarray:
方法如下所示:

[self setFontName:@"Roboto-Light" onItemsInArray:[self lightFontItems]];
[self setFontName:@"Roboto-Regular" onItemsInArray:[self regularFontItems]];
+ (void)setFontName:(NSString *)fontName onItemsInArray:(NSArray *)array;
{
  [array each:^(id item) {
    if (![item respondsToSelector:@selector(setFont:)]) return;
    [item performSelector:@selector(setFont:) withObject:[UIFont fontWithName:fontName size:[[item font] pointSize]]];
  }];
}

您可以安装此脚本

使用xcode 5.1.1对我来说效果非常好。

Swizzle字体 如果使用UIFont类,解决方案通常是简单的。我认为最好选择一种像Helvetica Neue这样的合理字体,然后覆盖它。这允许您通过拉取映射将整个项目放回标准中。我已经想出了一个swizzle来实现这个目标,当我意识到我也这么做了,所以我把它捣碎了一点。正如我们将告诉您的,swizzling可能是危险的,但是考虑到UIFont API的绝对简单性,这种情况下的风险非常低。在我的例子中,它是为一个企业应用程序而做的,所以风险更低

UIFont+CustomFont.m类别
#导入
静态NSString*常量kFontMapPlist=@“FontMap”;
静态NSDictionary*_replacementFontDictionary=nil;
@实现UIFont(自定义字体)
静态void initializeReplacementFonts()
{
静态布尔初始化=否;
如果(已初始化)
返回;
初始化=是;
//具有字典从->到字体名称映射的Plist
NSURL*replacementFontMapURL=[[NSBundle mainBundle]URLForResource:kFontMapPlist,扩展名:@“plist”];
NSDictionary*replacementFontMap=[NSDictionary Dictionary WithContentsOfURL:replacementFontMapURL];
[UIFont setReplacementFontDictionary:replacementFontMap];
}
+(空)荷载
{
静态调度一次;
一次发送(一次发送)^{
initializeReplacementFonts();
SEL fontWithNameSizeSelector=@selector(fontWithName:size:);
SEL swizzledFontWithNameSizeSelector=@selector(clp_fontWithName:size:);
SwizzleClassMethod([UIFont类]、fontWithNameSizeSelector、swizzledFontWithNameSizeSelector);
SEL fontWithDescriptorSizeSelector=@selector(fontWithDescriptor:size:);
SEL SwizzledFontWithDescriptor选择器=@selector(clp_fontWithDescriptor:大小:);
SwizzleClassMethod([UIFont类]、fontWithDescriptorSizeSelector、swizzledfontWithDescriptorSelector);
});
}
void SwizzleClassMethod(类类、SEL originalSelector、SEL REPLACENTSELECTOR)
{
Class clazz=objc_getMetaClass(Class_getName(Class));
方法originalMethod=class_getClassMethod(clazz,originalSelector);
方法replacementMethod=class_getClassMethod(clazz,replacementSelector);
//如果不支持eixst,则添加方法
布尔-迪达德法=
类添加方法(clazz,
原选举人,
方法_getImplementation(替换方法),
方法_getTypeEncoding(replacementMethod));
if(didAddMethod){
类替换方法(clazz,
替换选择器,
方法_getImplementation(原始方法),
方法_getTypeEncoding(原始方法));
}否则{
方法交换实施(原始方法、替代方法);
}
}
#pragma标记-按描述符和名称调用切换字体
+(UIFont*)CLPfontWithDescriptor:(UIFontDescriptor*)描述符大小:(CGFloat)点大小
{
NSString*originalFontName=descriptor.fonttributes[UIFontDescriptorNameAttribute];
NSString*replacementFontName=_replacementFontDictionary[originalFontName];
UIFontDescriptor*replacementFontDescriptor=描述符;
if(replacementFontName!=nil){
replacementFontDescriptor=[UIFontDescriptor fontDescriptorWithFontAttributes:@{UIFontDescriptorNameAttribute:replacementFontName}];
}
返回[self clp_fontWithDescriptor:replacementFontDescriptor大小:pointSize];
}
+(UIFont*)clp\U fontWithName:(NSString*)fontName大小:(CGFloat)fontSize
{
NSString*replacementFontName=\u replacementFontDictionary[fontName];
如果(replacementFontName==nil){
replacementFontName=fontName;
}
返回[self clp_fontWithName:replacementFontName大小:fontSize];
}
#pragma标记-替换字典Getter和Setter
+(NSDictionary*)替换字典
{
返回_replacementFontDictionary;
}
+(void)setReplacementFontDictionary:(NSDictionary*)ReplacementFontDictionary
{
如果(replacmentFontDictionary==\u replacementFontDictionary){
返回;
}
_replacementFontDictionary=replacementFontDictionary;
//验证字体是否存在。
for(NSString*originalFontName在[\u replacementFontDictionary allKeys]中){
NSString*replacementFontName=[\u replacementFontDictionary objectForKey:originalFontName];
UIFont*replacementFont=[UIFont-fontWithName:replacementFontName-size:10.0f];
如果(replacementFont==nil){
DDLogError(@“警告:替换字体
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>HelveticaNeue-Light</key>
    <string>CustomSans-Light</string>
    <key>HelveticaNeue-LightItalic</key>
    <string>CustomSans-LightItalic</string>
    <key>HelveticaNeue-Bold</key>
    <string>CustomSans-Bold</string>
    <key>HelveticaNeue-BoldItalic</key>
    <string>CustomSans-BoldItalic</string>
</dict>
</plist>