我们扩展了UILabel,以便能够在我们的应用程序中将标准字体和颜色应用于给定标签类型的所有用途。
@interface UILabelHeadingBold : UILabel
@end
在我们的应用程序代表中,我们应用这样的字体和颜色
[[UILabelHeadingBold appearance] setTextColor:<some color>];
[[UILabelHeadingBold appearance] setFont:<some font>];
在 XIB 中添加 UILabel 时,我们现在可以选择类型为 UILabelHeadingBold 的类,它按预期工作。标签以正确的字体和颜色显示,如我们的应用程序委托中所述。
但是,如果我们以编程方式创建标签,例如。
UILabelHeadingBold *headingLabel = [[UILabelHeadingBold alloc] initWithFrame:CGRectMake(10, 10, 100, 30)];
[self.mainView addSubview:headingLabel];
UILabel 未应用预期的字体/颜色。我们必须手动应用这些属性。
有没有办法使 UIAppearance 对编程创建的 UI 元素生效,还是仅在 XIB 中使用时才有效?
来自 Apple 文档 :
若要支持外观自定义,类必须符合 UIAppearance容器协议和相关访问器方法必须是 标有UI_APPEARANCE_SELECTOR。
例如在UINavigationBar.h
中,tintColor
用UI_APPEARANCE_SELECTOR
@property(nonatomic,retain) UIColor *tintColor UI_APPEARANCE_SELECTOR;
但是在UILabel.h中,您可以看到textColor
和font
属性没有标有UI_APPEARANCE_SELECTOR但是当添加到界面生成器中时,它以某种方式工作(按照文档,它根本不应该工作)。
我有用的简单技巧没有问题,是使用修改 UILabel 属性的 UIAppearance setter 创建一个类别。
按照 UIAppearance 约定,我创建了一个方法:
- (void)setTextAttributes:(NSDictionary *)numberTextAttributes;
{
UIFont *font = [numberTextAttributes objectForKey:UITextAttributeFont];
if (font) {
self.font = font;
}
UIColor *textColor = [numberTextAttributes objectForKey:UITextAttributeTextColor];
if (textColor) {
self.textColor = textColor;
}
UIColor *textShadowColor = [numberTextAttributes objectForKey:UITextAttributeTextShadowColor];
if (textShadowColor) {
self.shadowColor = textShadowColor;
}
NSValue *shadowOffsetValue = [numberTextAttributes objectForKey:UITextAttributeTextShadowOffset];
if (shadowOffsetValue) {
UIOffset shadowOffset = [shadowOffsetValue UIOffsetValue];
self.shadowOffset = CGSizeMake(shadowOffset.horizontal, shadowOffset.vertical);
}
}
在 UILabel 类别中:
@interface UILabel (UISS)
- (void)setTextAttributes:(NSDictionary *)numberTextAttributes UI_APPEARANCE_SELECTOR;
@end
我仍在试图弄清楚为什么原始二传手不起作用。
我遇到了完全相同的问题,但在 Swift 中。 自定义UILabel
的外观如果从情节提要添加,则不起作用,但如果从代码添加,则不起作用。
这是我在 Swift 中找到的一个对我有用的解决方案:
class MyLabel: UILabel { }
extension UILabel {
@objc dynamic var customFont: UIFont! {
get { return self.font }
set { self.font = newValue }
}
@objc dynamic var customColor: UIColor! {
get { return self.textColor }
set { self.textColor = newValue }
}
}
然后在配置应用外观的位置添加以下行:
MyLabel.appearance().customFont = UIFont.systemFont(ofSize: 20)
MyLabel.appearance().customColor = UIColor.magenta
> @robert.wijas 解决方案效果很好!
对于iOS 7及更高版本,我必须更新密钥,因为他使用的密钥已被7+弃用:
- (void)setTextAttributes:(NSDictionary *)numberTextAttributes;
{
UIFont *font = [numberTextAttributes objectForKey:NSFontAttributeName];
if (font) {
self.font = font;
}
UIColor *textColor = [numberTextAttributes objectForKey:NSForegroundColorAttributeName];
if (textColor) {
self.textColor = textColor;
}
UIColor *textShadowColor = [numberTextAttributes objectForKey:NSShadowAttributeName];
if (textShadowColor) {
self.shadowColor = textShadowColor;
}
NSValue *shadowOffsetValue = [numberTextAttributes objectForKey:NSShadowAttributeName];
if (shadowOffsetValue) {
UIOffset shadowOffset = [shadowOffsetValue UIOffsetValue];
self.shadowOffset = CGSizeMake(shadowOffset.horizontal, shadowOffset.vertical);
}
}
我使用的一种解决方法是手动应用设置的外观中的颜色:
let label = UILabel()
label.textColor = UILabel.appearance().textColor
这样,您就不需要引用任何新内容,也不需要显式定义颜色。这也适用于特定于上下文的着色:
label.textColor = UILabel.appearance(whenContainedInInstancesOf:[MyView.self]).textColor