[英]Swift - create array of custom colors
Is there a way to create an array of custom colors?有没有办法创建一组自定义颜色? I know I can create custom colors in
class UIColors
but in my case I need an array of my custom colors.我知道我可以在
class UIColors
创建自定义颜色,但就我而言,我需要一组自定义颜色。
I would like to achieve something like this:我想实现这样的目标:
let listColors: [UIColor] = [
let color1 = UIColor(displayP3Red: 1, green: 1, blue: 1, alpha: 1),
let color2 = UIColor(displayP3Red: 2, green: 2, blue: 2, alpha: 1),
let color3 = UIColor(displayP3Red: 3, green: 3, blue: 3, alpha: 1),
]
You don't need a subclass to create an array do您不需要子类来创建数组
let listColors = [
UIColor(displayP3Red: 1, green: 1, blue: 1, alpha: 1),
UIColor(displayP3Red: 2, green: 2, blue: 2, alpha: 1),
UIColor(displayP3Red: 3, green: 3, blue: 3, alpha: 1)
]
access使用权
listColors[index]
or或者
class Colors {
static let color1 = UIColor(displayP3Red: 1, green: 1, blue: 1, alpha: 1)
static let color2 = UIColor(displayP3Red: 2, green: 2, blue: 2, alpha: 1)
static let color3 = UIColor(displayP3Red: 3, green: 3, blue: 3, alpha: 1)
}
access使用权
Colors.color1
or as global或作为全球
let color1 = UIColor(displayP3Red: 1, green: 1, blue: 1, alpha: 1)
let color2 = UIColor(displayP3Red: 2, green: 2, blue: 2, alpha: 1)
let color3 = UIColor(displayP3Red: 3, green: 3, blue: 3, alpha: 1)
access使用权
color1
When you create a color divide by 255
当您创建颜色除以 255 时
UIColor(displayP3Red: 2/255.0, green: 2/255.0, blue: 2/255.0, alpha: 1)
Or if you want to be able to reference them by name or by array index:或者,如果您希望能够通过名称或数组索引引用它们:
let color1 = UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0), //White
let color2 = UIColor(red: 0.5, green: 0.5, blue: 0.5, alpha: 1.0), //Gray
let color3 = UIColor(red: 1.0, green: 0.0, blue: 0.0, alpha: 1.0), //Red
let listColors: [UIColor] = [color1, color2, color3]
If you are supporting dark and light modes in your App, it is best to define your custom colors as assets in your App and then access them something like this如果您的应用程序支持深色和浅色模式,最好将自定义颜色定义为应用程序中的资产,然后像这样访问它们
extension String {
static let blueColorName = "Blue"
static let brownColorName = "Brown"
static let crustaColorName = "Crusta"
static let darkRedColorName = "DarkRed"
static let defaultColorName = "Default"
static let flamingoColorName = "Tango"
static let khakiColorName = "Khaki"
static let greenColorName = "Green"
static let limeColorName = "Lime"
static let lustColorName = "Lust"
static let oliveColorName = "Olive"
static let orangeColorName = "Orange"
static let pinkColorName = "Pink"
static let purpleColorName = "Purple"
static let roseColorName = "Rose"
static let redColorName = "Red"
static let skyColorName = "Sky"
static let steelColorName = "Steel"
static let sunColorName = "Sun"
static let tealColorName = "Teal"
static let yellowColorName = "Yellow"
// Tile Background Colors
static let tileGrayColorName = "TileGray"
static let tileGreenColorName = "TileGreen"
static let tileRedColorName = "TileRed"
static let tileYellowColorName = "TileYellow"
}
extension UIColor {
// MARK: - Custom Color List For Color Picker
static let customColorsList: [(colorName: String, uiColor: UIColor)] = [
("Orange", UIColor(named: .orangeColorName)!),
("Tango", UIColor(named: .flamingoColorName)!),
("Crusta", UIColor(named: .crustaColorName)!),
("Yellow", UIColor(named: .yellowColorName)!),
("Sun", UIColor(named: .sunColorName)!),
("Khaki", UIColor(named: .khakiColorName)!),
("Lime", UIColor(named: .limeColorName)!),
("Green", UIColor(named: .greenColorName)!),
("Olive", UIColor(named: .oliveColorName)!),
("Teal", UIColor(named: .tealColorName)!),
("Sky", UIColor(named: .skyColorName)!),
("Blue", UIColor(named: .blueColorName)!),
("Steel", UIColor(named: .steelColorName)!),
("Pink", UIColor(named: .pinkColorName)!),
("Rose", UIColor(named: .roseColorName)!),
("Purple", UIColor(named: .purpleColorName)!),
("Lust", UIColor(named: .lustColorName)!),
("Dark Red", UIColor(named: .darkRedColorName)!),
("Brown", UIColor(named: .brownColorName)!),
("Default", UIColor(named: .defaultColorName)!)
]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.