我的应用程序中有许多 UILabel 元素,我正在寻找一种简单的方法来使用 UIAppearance 代理对象设置它们的样式。我目前的方法是创建配备 UI_APPEARANCE_SELECTOR 装饰器的 UILabel 的不同子类,以便可以通过[UILabelSubclass appearance]
调用访问。您可以在此处和此处找到我用作参考的资源。问题是我不想设置字体大小,而只想设置依赖于情节提要中定义的大小的字体系列。
如何在子类中设置它?
这是一个适用于 UIAppearance 代理的简单类别:
@interface UILabel (FontName)
- (void)setFontName:(NSString *)name UI_APPEARANCE_SELECTOR;
@end
@implementation UILabel (FontName)
- (void)setFontName:(NSString *)name {
self.font = [UIFont fontWithName:name size:self.font.pointSize];
}
@end
Thanks to this post, and also a blog post from Peter Steinberger (http://petersteinberger.com/) I was able to get something together that works for me:
@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
我认为您可以在您的子类中尝试以下操作:
UIFont *newFont = [UIFont fontWithName:@"YourFontName" size:self.font.pointSize];
self.font = newFont