自定义调色板Swift

时间:2015-11-06 02:55:49

标签: ios swift swift2

我在this tutorial之后创建了一个自定义调色板。

我如何以编程方式使用自定义颜色?

2 个答案:

答案 0 :(得分:3)

首先,只需将swatch文件复制(添加)到项目资源中。

// get plist file URL
if let plistURL = NSBundle.mainBundle().URLForResource("NSColorPanelSwatches", withExtension: "plist") {
    // load plist data
    if let plistData = NSData(contentsOfURL: plistURL) {
        // decode the colors stored at your swatches plist file
        if let colorsArray = NSKeyedUnarchiver(forReadingWithData: plistData).decodeObjectForKey("NSSwatchColorArray") as? [UIColor] {
            for color in colorsArray {
                print(color.description)
            }
        }
    }
}

答案 1 :(得分:0)

我想你想要阅读.clr文件而不是~/Library/Colors的plist。如果是,请使用NSColorList

let colorList = NSColorList(name: "mylist", fromFile: "/path/to/file.clr")!
let color = colorList.colorWithKey("my color name") // return an NSColor

my color name是你在那个颜色里面给出的名字。

相关问题