Ios 使用UIAppearance代理设置UILabel的样式

Ios 使用UIAppearance代理设置UILabel的样式,ios,uilabel,uifont,uiappearance,Ios,Uilabel,Uifont,Uiappearance,我的应用程序中有许多UILabel元素,我正在寻找一种使用UIAppearance代理对象为它们设置样式的简单方法。我目前的方法是创建UILabel的不同子类,这些子类配备有一个UI\u外观\u选择器装饰器,以便通过[UILabelSubclass外观]调用进行访问。你可以找到我用作参考的资料来源。 问题是我不想设置字体大小,只想设置依赖于情节提要中定义的大小的字体系列 如何在子类中设置此选项?我认为您可以在子类中尝试以下操作: UIFont *newFont = [UIFont fontWit

我的应用程序中有许多UILabel元素,我正在寻找一种使用UIAppearance代理对象为它们设置样式的简单方法。我目前的方法是创建UILabel的不同子类,这些子类配备有一个UI\u外观\u选择器装饰器,以便通过
[UILabelSubclass外观]
调用进行访问。你可以找到我用作参考的资料来源。 问题是我不想设置字体大小,只想设置依赖于情节提要中定义的大小的字体系列


如何在子类中设置此选项?

我认为您可以在子类中尝试以下操作:

UIFont *newFont = [UIFont fontWithName:@"YourFontName" size:self.font.pointSize];
self.font = newFont

以下是一个与UIAppearance代理一起使用的简单类别:

@接口UILabel(FontName)
-(void)setFontName:(NSString*)名称UI\u外观\u选择器;
@结束
@实现UILabel(FontName)
-(void)setFontName:(NSString*)名称{
self.font=[UIFont-fontWithName:name-size:self.font.pointSize];
}
@结束

多亏了这篇文章,以及Peter Steinberger()的一篇博文,我才得以找到适合我的东西:

@interface UILabel (FontAppearance)
@property (nonatomic, copy) UIFont *appearanceFont UI_APPEARANCE_SELECTOR;
@end


@implementation UILabel (FontAppearance)
- (void)setAppearanceFont:(UIFont*)font
{
    [self setFont:font];
}

-(UIFont*)appearanceFont
{
    return [self font];
}
@end

这也是一个很好的解决方案。我标记了另一个,只是因为它离我要实现的有点近。