我在这个网站上搜索过,但只找到了未回答的问题。
我将自定义字体加载到了我的xcode项目中,[UIFont fontWithName:@"Laconic-Light" size:19]
可以正常工作。但界面构建器不喜欢这种字体。我不能在IB中使用它,因为它总是显示默认字体。有没有办法告诉IB可以使用这种字体?
我在这个网站上搜索过,但只找到了未回答的问题。
我将自定义字体加载到了我的xcode项目中,[UIFont fontWithName:@"Laconic-Light" size:19]
可以正常工作。但界面构建器不喜欢这种字体。我不能在IB中使用它,因为它总是显示默认字体。有没有办法告诉IB可以使用这种字体?
我在Xcode 4中也遇到了同样的问题。在我的程序中,有很多没有IBOutlets
的UILabel
,所以我采用了以下方法:
首先,将UILabel
子类化为CustomFontLabel
然后,覆盖"awakeFromNib
"方法
@implementation CustomFontLabel
- (void)awakeFromNib {
[super awakeFromNib];
self.font = [UIFont fontWithName:@"CustomFontName" size:self.font.pointSize];
}
@end
最后,在Interface Builder > Identity Inspector中将类更改为CustomFontLabel
。
另一个解决方案是通过子类化UILabel来加载您的自定义字体。然后,您可以在IB中引用它,尽管您仍然无法看到正确的字体。
我更喜欢以稍微通用的方式来实现这个,这样可以让您在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]]];
}];
}
[self.lightFontItems setValue:[UIFont fontWithName:@"Roboto-Light" size:12] forKey:@"font"]
。 (KVC是键值编码的缩写,它是一种在Objective-C中非常常用的机制,用于通过字符串键或路径访问对象属性或成员变量。此处代码片段使用KVC机制将“font”属性设置为指定字体和大小的UIFont对象。) - Barum Rho如果您混淆UIFont类,则解决方案通常很简单。我认为最好选择一个明智的字体,如Helvetica Neue,并覆盖它。这样,您就可以通过拉取映射将整个项目放回标准状态。当我意识到其他人也这样做时,我已经想出了一种混淆来实现这个目标,所以我将其混合在一起。正如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>