我搜索了这个网站,但我发现了一些悬而未决的问题。
我已将自定义字体加载到我的xcode项目中。 [UIFont fontWithName:@"Laconic-Light" size:19]
有效。但是界面构建器不喜欢该字体。我无法与IB一起使用它总是显示默认字体。有没有办法告诉IB可以使用该字体?
答案 0 :(得分:63)
我在Xcode 4中也有这个问题。在我的程序中,有很多UILabel
没有IBOutlets
所以我这样做了;
首先,将UILabel
继承为CustomFontLabel
然后,覆盖“awakeFromNib
”方法
@implementation CustomFontLabel
- (void)awakeFromNib {
[super awakeFromNib];
self.font = [UIFont fontWithName:@"CustomFontName" size:self.font.pointSize];
}
@end
最后,在Interface Builder> Identity Inspector将类更改为CustomFontLabel
。
答案 1 :(得分:5)
另一个解决方案是将UILabel子类化为加载自定义字体。然后,您可以在IB中引用它,尽管您仍然看不到正确的字体。
答案 2 :(得分:3)
我更喜欢以稍微更通用的方式执行此操作,这样您可以在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]];
setLightFontOnItemsInArray:
方法如下所示:
+ (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]]];
}];
}
答案 3 :(得分:3)
您可以安装此脚本http://pitaya.ch/moarfonts/。
使用xcode 5.1.1非常适合我。
答案 4 :(得分:3)
感谢Apple,在Xcode 6中,我们在界面构建器本身中提供了自定义字体。
答案 5 :(得分:1)
如果您调用UIFont类,解决方案通常很简单。我认为最好选择像Helvetica Neue这样的理智字体并覆盖它。这允许您通过拉动映射将整个项目重新置于标准中。当我意识到someone else也这样做时,我已经想出了一个实现这一目标的调酒,所以我把它捣碎了一下。由于NSHipster会告诉你调配可能很危险,但在这种情况下,考虑到UIFont API的绝对简单性,风险非常低。在我的情况下,它是为企业应用程序完成的,因此风险更低。
#import <objc/runtime.h>
static NSString *const kFontMapPlist = @"FontMap";
static NSDictionary *_replacementFontDictionary = nil;
@implementation UIFont (CustomFont)
static void initializeReplacementFonts()
{
static BOOL initialized = NO;
if (initialized)
return;
initialized = YES;
// A Plist with a Dictionary from->to font name mapping
NSURL *replacementFontMapURL = [[NSBundle mainBundle] URLForResource:kFontMapPlist withExtension:@"plist"];
NSDictionary *replacementFontMap = [NSDictionary dictionaryWithContentsOfURL:replacementFontMapURL];
[UIFont setReplacementFontDictionary:replacementFontMap];
}
+ (void)load
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
initializeReplacementFonts();
SEL fontWithNameSizeSelector = @selector(fontWithName:size:);
SEL swizzledFontWithNameSizeSelector = @selector(clp_fontWithName:size:);
SwizzleClassMethod([UIFont class], fontWithNameSizeSelector, swizzledFontWithNameSizeSelector);
SEL fontWithDescriptorSizeSelector = @selector(fontWithDescriptor:size:);
SEL swizzledfontWithDescriptorSelector = @selector(clp_fontWithDescriptor:size:);
SwizzleClassMethod([UIFont class], fontWithDescriptorSizeSelector, swizzledfontWithDescriptorSelector);
});
}
void SwizzleClassMethod(Class class, SEL originalSelector, SEL replacementSelector)
{
Class clazz = objc_getMetaClass(class_getName(class));
Method originalMethod = class_getClassMethod(clazz, originalSelector);
Method replacementMethod = class_getClassMethod(clazz, replacementSelector);
// Add method if it doesn't eixst
BOOL didAddMethod =
class_addMethod(clazz,
originalSelector,
method_getImplementation(replacementMethod),
method_getTypeEncoding(replacementMethod));
if (didAddMethod) {
class_replaceMethod(clazz,
replacementSelector,
method_getImplementation(originalMethod),
method_getTypeEncoding(originalMethod));
} else {
method_exchangeImplementations(originalMethod, replacementMethod);
}
}
#pragma mark - Swizzled font by descriptor and name calls
+ (UIFont *)clp_fontWithDescriptor:(UIFontDescriptor *)descriptor size:(CGFloat)pointSize
{
NSString *originalFontName = descriptor.fontAttributes[UIFontDescriptorNameAttribute];
NSString *replacementFontName = _replacementFontDictionary[originalFontName];
UIFontDescriptor *replacementFontDescriptor = descriptor;
if (replacementFontName != nil) {
replacementFontDescriptor = [UIFontDescriptor fontDescriptorWithFontAttributes:@{UIFontDescriptorNameAttribute: replacementFontName}];
}
return [self clp_fontWithDescriptor:replacementFontDescriptor size:pointSize];
}
+ (UIFont *)clp_fontWithName:(NSString *)fontName size:(CGFloat)fontSize
{
NSString *replacementFontName = _replacementFontDictionary[fontName];
if (replacementFontName == nil) {
replacementFontName = fontName;
}
return [self clp_fontWithName:replacementFontName size:fontSize];
}
#pragma mark - Replacement Dictionary Getter and Setter
+ (NSDictionary *)replacementDictionary
{
return _replacementFontDictionary;
}
+ (void)setReplacementFontDictionary:(NSDictionary *)replacmentFontDictionary
{
if (replacmentFontDictionary == _replacementFontDictionary) {
return;
}
_replacementFontDictionary = replacmentFontDictionary;
// Validate font existence.
for (NSString *originalFontName in [_replacementFontDictionary allKeys]) {
NSString *replacementFontName = [_replacementFontDictionary objectForKey:originalFontName];
UIFont *replacementFont = [UIFont fontWithName:replacementFontName size:10.0f];
if (replacementFont == nil) {
DDLogError(@"WARNING: replacement font '%@' is not available.", replacementFontName);
}
}
}
@end
为了简单起见,我们假装我们有一个名为CustomSans的字体。
<?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>