如何使用资产目录颜色集?

时间:2021-11-01 00:54:12

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?

我通常在iOS上使用自定义UIColors,使用Swift扩展,但是现在有了iOS11/XCode9,我们可以创建颜色集。我们如何使用它们?

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:

@Cœur说我们可以进行拖拽de颜色,并使用它就像一个用户界面颜色对象和一个可能的解决方案可以使用它作为一个扩展:

如何使用资产目录颜色集?

Or as a constant:

或作为一个常数:

如何使用资产目录颜色集?

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

现在我想知道我们是否可以像UIImage一样访问一个资产图像,比如:

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

4 个解决方案

#1


33  

UIColor(named: "myColor") 

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

来源:WWDC 2017会话237——MapKit中的新内容。


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

注意:您的项目的部署目标需要设置为iOS 11.0。

#2


27  

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

(对问题更新的简短回答:在Xcode 9.0中有UIColor(命名为“MyColor”))

Answering the original question:

回答最初的问题:

  1. you create your color set
  2. 您创建您的颜色集

如何使用资产目录颜色集?

  1. you find your color among your snippets and you drag-n-drop it
  2. 你在你的代码片段中找到你的颜色,然后把它拖放。

如何使用资产目录颜色集?

  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)

    #colorLiteral(红色:0,绿色:0.6378085017,蓝色: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.

你注意到红色,绿色和蓝色的值是如何不同的吗?因为我使用颜色空间显示P3来定义它们,但是colorLiteral使用颜色空间sRGB。

#3


8  

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").

向资产目录添加一个颜色集,命名它并在属性检查器中设置颜色,然后在代码中使用UIColor(命名为:“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

    在asset catalog查看器中,单击主面板右下角的加号按钮,并选择新的颜色集

    如何使用资产目录颜色集?

  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.

    从那里你可以命名和选择你的颜色。

    如何使用资产目录颜色集?

  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. 要在代码中使用它,可以使用UIColor(命名为:“MyColor”)调用它。这返回一个可选的,因此在大多数情况下您需要展开它(考虑到您知道您的资产目录中存在颜色,这可能是少数几个可以接受强制展开的情况之一)。

#4


2  

 // iOS
 let color = UIColor(named: "SillyBlue")

 // macOS
 let color = NSColor(named: "SillyBlue")

#1


33  

UIColor(named: "myColor") 

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

来源:WWDC 2017会话237——MapKit中的新内容。


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

注意:您的项目的部署目标需要设置为iOS 11.0。

#2


27  

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

(对问题更新的简短回答:在Xcode 9.0中有UIColor(命名为“MyColor”))

Answering the original question:

回答最初的问题:

  1. you create your color set
  2. 您创建您的颜色集

如何使用资产目录颜色集?

  1. you find your color among your snippets and you drag-n-drop it
  2. 你在你的代码片段中找到你的颜色,然后把它拖放。

如何使用资产目录颜色集?

  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)

    #colorLiteral(红色:0,绿色:0.6378085017,蓝色: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.

你注意到红色,绿色和蓝色的值是如何不同的吗?因为我使用颜色空间显示P3来定义它们,但是colorLiteral使用颜色空间sRGB。

#3


8  

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").

向资产目录添加一个颜色集,命名它并在属性检查器中设置颜色,然后在代码中使用UIColor(命名为:“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

    在asset catalog查看器中,单击主面板右下角的加号按钮,并选择新的颜色集

    如何使用资产目录颜色集?

  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.

    从那里你可以命名和选择你的颜色。

    如何使用资产目录颜色集?

  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. 要在代码中使用它,可以使用UIColor(命名为:“MyColor”)调用它。这返回一个可选的,因此在大多数情况下您需要展开它(考虑到您知道您的资产目录中存在颜色,这可能是少数几个可以接受强制展开的情况之一)。

#4


2  

 // iOS
 let color = UIColor(named: "SillyBlue")

 // macOS
 let color = NSColor(named: "SillyBlue")