簡體   English   中英

Swift - 創建自定義顏色數組

[英]Swift - create array of custom colors

有沒有辦法創建一組自定義顏色? 我知道我可以在class UIColors創建自定義顏色,但就我而言,我需要一組自定義顏色。

我想實現這樣的目標:

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),
]

您不需要子類來創建數組

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)
]

使用權

listColors[index]

或者

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)

}

使用權

Colors.color1

或作為全球

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)

使用權

color1

當您創建顏色除以 255 時

UIColor(displayP3Red: 2/255.0, green: 2/255.0, blue: 2/255.0, alpha: 1)

或者,如果您希望能夠通過名稱數組索引引用它們:

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]

如果您的應用程序支持深色和淺色模式,最好將自定義顏色定義為應用程序中的資產,然后像這樣訪問它們

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM