所有UILabel的默认文本颜色 [英] The default text color for all UILabels

查看:187
本文介绍了所有UILabel的默认文本颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为我的应用中的所有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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆