How can I make a Swift enum with UIColor value?

前端 未结 9 745
深忆病人
深忆病人 2020-12-16 09:37

I\'m making a drawing app and I would like to refer to my colors through use of an enum. For example, it would be cleaner and more convenient to use Colors.RedColor

9条回答
  •  鱼传尺愫
    2020-12-16 10:02

    I do it like this (basically using a struct as a namespace):

    extension UIColor {
      struct MyTheme {
        static var firstColor: UIColor  { return UIColor(red: 1, green: 0, blue: 0, alpha: 1) }
        static var secondColor: UIColor { return UIColor(red: 0, green: 1, blue: 0, alpha: 1) }
      }
    }
    

    And you use it like:

    UIColor.MyTheme.firstColor
    

    So you can have a red color inside your custom theme.

提交回复
热议问题