所有UILabel的默认文本颜色 [英] The default text color for all UILabels
问题描述
我想为我的应用中的所有UILabel设置默认文本颜色。我发现了这个:
UILabel.appearance()。textColor = UIColor.red
这样可行,但是当我想在其故事板中为特定标签设置其他颜色时,无法完成 - 所有标签仍为红色。 / p>
是否可以通过在故事板中设置所有UILabel来定义默认文本颜色,然后在故事板(而非代码)中为某些UILabel更改它?或者使用Appearance API在代码中设置默认颜色,并为storyboard中的某些标签更改它?
使用CustomLabel标记的子类。迅速。您可以使用 IBDesignable
属性设置文本颜色,该属性名为 txtColor
下面是代码工作示例
import UIKit
@IBDesignable class CustomLabel:UILabel {
@IBInspectable var txtColor:UIColor = UIColor.grayColor(){
didSet {
self.textColor = txtColor
}
}
func setup(){
self.textColor = txtColor
}
覆盖func awakeFromNib(){
setup()
}
覆盖func prepareForInterfaceBuilder(){
setup()
}
}
I want to set the default text color for all UILabels in my app. I found this:
UILabel.appearance().textColor = UIColor.red
This works, but when I want to set some other color for a particular label in its storyboard, it cannot be done - all labels are still red.
Is it possible to define a default text color for all UILabels by setting it in storyboards and then change it for some UILabels ALSO in storyboards (not in code)? Or set the default color in code using the Appearance API and change it for some labels in storyboard?
SubClass you labels with CustomLabel.swift. You can set text color using IBDesignable
property named as txtColor
Below is the code working example
import UIKit
@IBDesignable class CustomLabel: UILabel {
@IBInspectable var txtColor: UIColor = UIColor.grayColor() {
didSet {
self.textColor = txtColor
}
}
func setup() {
self.textColor = txtColor
}
override func awakeFromNib() {
setup()
}
override func prepareForInterfaceBuilder() {
setup()
}
}
这篇关于所有UILabel的默认文本颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!