我有一个CustomColors
结构,其中包含static let
个定义我的应用的主要和次要颜色的结构。
struct CustomColors {
static let orangePrimary = UIColor(r: 255, g: 110, b: 64, a: 1)
static let orangeSecondary = UIColor(r: 230, g: 74, b: 25, a: 1)
static let bluePrimary = UIColor(r: 24, g: 255, b: 255, a: 1)
static let blueSecondary = UIColor(r: 18, g: 191, b: 191, a: 1)
static let yellowPrimary = UIColor(r: 255, g: 255, b: 0, a: 1)
static let yellowSecondary = UIColor(r: 255, g: 193, b: 7, a: 1)
}
因为二级颜色完全依赖于主要颜色,我正在寻找一种方法来构建一个数据结构(以最快的方式),这将允许我调用例如:CustomColors.bluePrimary.secondary
来获取{{ 1}}颜色。
我觉得我在这里遗漏了一些非常基本的东西,因为我似乎无法看到我能以最简单的方式做到这一点。
提前感谢您的帮助!
答案 0 :(得分:1)
为您的颜色创建另一个struct
并创建您自己的类型。 RGB
值为float
。值应除以255.0
struct CustomColors {
struct Blue {
static let primary = UIColor(colorLiteralRed: 24.0/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1.0)
static let secondary = UIColor(colorLiteralRed: 18.0/255.0, green: 191.0/255.0, blue: 191/255.0, alpha: 1.0)
}
struct Orange {
static let primary = UIColor(colorLiteralRed: 255.0/255.0, green: 110.0/255.0, blue: 64/255.0, alpha: 1.0)
static let secondary = UIColor(colorLiteralRed: 230.0/255.0, green: 74.0/255.0, blue: 25/255.0, alpha: 1.0)
}
struct Yellow {
static let primary = UIColor(colorLiteralRed: 255.0/255.0, green: 255.0/255.0, blue: 0.0, alpha: 1.0)
static let secondary = UIColor(colorLiteralRed: 255.0/255.0, green: 193.0/255.0, blue: 7/255.0, alpha: 1.0)
}
}
现在您可以访问CustomColors.Blue.primary
let blueColor = CustomColors.Blue.primary
答案 1 :(得分:1)
对颜色使用离散属性并不是特别可扩展;您正在处理代码中的特定字符串,这些字符串在运行时很难解决。
我建议您扩展结构以包含颜色对结构和枚举以访问适当的颜色对。由于枚举不符合hashable
,因此您无法直接将其用作字典键,但我们可以使用字符串原始值来绕过它。
类似的东西:
struct CustomColors {
struct Colors {
let primary: UIColor
let secondary: UIColor
}
enum Theme:String {
case orange
case blue
case yellow
}
private let colors: [String:Colors] = [
Theme.orange.rawValue: Colors(primary: UIColor(r: 255, g: 110, b: 64, a: 1),secondary: UIColor(r: 230, g: 74, b: 25, a: 1)),
Theme.blue.rawValue: Colors(primary:UIColor(r: 24, g: 255, b: 255, a: 1), secondary:UIColor(r: 24, g: 255, b: 255, a: 1)),
Theme.yellow.rawValue: Colors(primary: UIColor(r: 255, g: 255, b: 0, a: 1), secondary: UIColor(r: 255, g: 193, b: 7, a: 1))
]
func colorFor(theme: Theme) -> Colors {
return colors[theme.rawValue]!
}
}
现在,您可以跟踪当前活动的主题并使用变量来访问颜色。获得Colors
结构后,只需访问primary
和secondary
属性
let cc = CustomColors()
let activeTheme: CustomColors.Theme = .blue
print(cc.colorFor(theme: activeTheme).primary)
您还可以使用原始值轻松识别并创建Theme
print(activeTheme)
if let someOtherTheme = CustomColors.Theme(rawValue: "yellow") {
print(someOtherTheme)
}
答案 2 :(得分:0)
extension UIColor {
open class var orangePrimary: UIColor {
return UIColor.orange
}
}
您可以使用UIcolor.orangePrimary
答案 3 :(得分:0)
尝试这种方式:
struct CustomColors {
public class Color: UIColor {
private var _secondary:UIColor?
var secondary:UIColor? {
get {
return _secondary
}
}
func setSecondary(red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat) -> Color {
_secondary = UIColor(red: red, green: green, blue: blue, alpha: alpha)
return self
}
}
static let orangeColor = Color(red: 255, green: 110, blue: 64, alpha: 1).setSecondary(red: 230, green: 74, blue: 25, alpha: 1)
static let blueColor = Color(red: 24, green: 255, blue: 255, alpha: 1).setSecondary(red: 18, green: 255, blue: 255, alpha: 1)
static let yellowColor = Color(red: 255, green: 255, blue: 0, alpha: 1).setSecondary(red: 255, green: 193, blue: 7, alpha: 1)
}
CustomColors.orangeColor.secondary
这是实现您想要的更灵活的方式,我想:)