How can we use Assets Catalog Colors Sets

2019-01-11 21:42发布

I usually use custom UIColors on iOS using extensions with Swift but now with iOS11/XCode9 we can create Colors Sets. How can we use them?

Update - Tip

As @Cœur says we can drag&drop de color, and use it like a UIColor object and a possible solution could be use it as a extension:

UIColor as an extension

Or as a constant:

UIColor as a constant

Now I wanna know if we can access them like an UIImage access to an Asset Image or not, like:

UIImage(named: "image-name") -> UIColor(named: "color-name")

5条回答
等我变得足够好
2楼-- · 2019-01-11 21:55

(short answer to the question update: there is UIColor(named: "MyColor") in Xcode 9.0)

Answering the original question:

  1. you create your color set

enter image description here

  1. you find your color among your snippets and you drag-n-drop it

enter image description here

  1. it will translate to a color literal when looking at the source code:

    #colorLiteral(red: 0, green: 0.6378085017, blue: 0.8846047521, alpha: 1)

You notice how the values of red, green and blue are different? It's because I defined them using Color Space Display P3, but the colorLiteral is using Color Space sRGB.

查看更多
再贱就再见
3楼-- · 2019-01-11 21:59

You need to use UIColor(named: "appBlue").

And you can create a function in UIColor extension for simple access.

enum AssetsColor {
case yellow
case black
case blue
case gray
case green
case lightGray
case seperatorColor
case rad
}

extension UIColor {

static func appColor(_ name: AssetsColor) -> UIColor? {
    switch name {
    case .yellow:
        return UIColor(named: "appYellow")
    case .black:
        return UIColor(named: "appBlack")
    case .blue:
        return UIColor(named: "appBlue")
    case .gray:
        return UIColor(named: "appGray")
    case .lightGray:
        return UIColor(named: "appLightGray")
    case .rad:
        return UIColor(named: "appRad")
    case .seperatorColor:
        return UIColor(named: "appSeperatorColor")
    case .green:
        return UIColor(named: "appGreen") 
    }
}

You can use it like this

userNameTextField.textColor = UIColor.appColor(.gray)
查看更多
淡お忘
4楼-- · 2019-01-11 22:07

Short Version

Add a colour set to an asset catalog, name it and set your colour in the attributes inspector, then call it in your code with UIColor(named: "MyColor").

Full Instructions

  1. In the asset catalog viewer, click the plus button at the bottom right of the main panel and choose New Color Set

    New Color Set menu

  2. Click on the white square, and select the Attributes Inspector (right-most icon in the right pane)

  3. From there you can name and choose your colour.

    enter image description here

  4. To use it in your code, call it with UIColor(named: "MyColor"). This returns an optional, so you'll need to unwrap it in most cases (this is probably one of the few cases where a force unwrap is acceptable, given you know the colour exists in your asset catalog).
查看更多
放我归山
5楼-- · 2019-01-11 22:08
UIColor(named: "myColor") 

Source: WWDC 2017 Session 237 —— What's New in MapKit


Caveat: Your project's Deployment Target needs to be set to iOS 11.0.

查看更多
唯我独甜
6楼-- · 2019-01-11 22:12
 // iOS
 let color = UIColor(named: "SillyBlue")

 // macOS
 let color = NSColor(named: "SillyBlue")
查看更多
登录 后发表回答