参见英文答案 How to change dynamically color used in UIStoryboard2个 我如何设置UILabel上的文本颜色,以及我的一个类中定义的颜色,如: func mainAppColor() - UIColor { return UIColor(red: 0.2, green: 0.3, blue: 0
我如何设置UILabel上的文本颜色,以及我的一个类中定义的颜色,如:
func mainAppColor() -> UIColor {
return UIColor(red: 0.2, green: 0.3, blue: 0.4, alpha: 1)
}
要么
let mainAppColor = UIColor(red: 0.2, green: 0.3, blue: 0.4, alpha: 1)
在故事板中.
如果您的方法在其他类中声明,例如:class TextColorClass {
func mainAppColor() -> UIColor {
return UIColor(red: 0.2, green: 0.3, blue: 0.4, alpha: 1)
}
}
然后你可以通过这种方式使用该类的实例来使用它:
let classInstance = TextColorClass() yourLBL.textColor = classInstance.mainAppColor()
如果声明如下:
class TextColorClass {
let mainAppColor = UIColor(red: 0.2, green: 0.3, blue: 0.4, alpha: 1)
}
然后你就可以使用它:
let classInstance = TextColorClass() yourLBL.textColor = classInstance.mainAppColor
如果在ViewController类中假设该方法在同一个类中声明,那么您可以这样使用它:
override func viewDidLoad() {
super.viewDidLoad()
yourLBL.textColor = mainAppColor()
}
func mainAppColor() -> UIColor {
return UIColor(red: 0.2, green: 0.3, blue: 0.4, alpha: 1)
}
更新:
如果要从storyboard设置任何属性,可以使用@IBInspectable,如下面的示例代码所示:
import UIKit
@IBDesignable
class PushButtonView: UIButton {
@IBInspectable var fillColor: UIColor = UIColor.greenColor()
@IBInspectable var isAddButton: Bool = true
override func drawRect(rect: CGRect) {
}
}
并将此类分配给您的按钮.如下图所示:

如果您转到属性检查器,您可以看到该按钮的自定义属性.

现在您可以根据需要进行设置.
有关更多信息,请参阅本教程:
http://www.raywenderlich.com/90690/modern-core-graphics-with-swift-part-1
