简体   繁体   English

如何以编程方式访问资产目录颜色集

[英]How to Access Asset Catalog colour set programmatically

I want to use a colour set within my code.我想在我的代码中使用一个颜色集。 i want the asset file to be read so that it can access the colour set.我希望读取资产文件,以便它可以访问颜色集。

so i have set my name of the colour set to bottomNavigation.所以我将我的颜色名称设置为bottomNavigation。

I have tried using UIColor(named: "bottomNavigation") but it does not pick up the colour.我试过使用UIColor(named: "bottomNavigation")但它没有选择颜色。

i know that you can reference an asset file by doing this for an image:我知道您可以通过为图像执行此操作来引用资产文件:

UIImage* image = [UIImage imageNamed:@"name-in-asset-catalog"];

But i'm not sure how you would do that for a colour or how to reference the Asset file to get the colour set但是我不确定您将如何为颜色执行此操作或如何引用资产文件以获取颜色集

I could not find an easier way, but can achieve it in the following manner我找不到更简单的方法,但可以通过以下方式实现

Press Cmd + Shift + L to open library and then select Color tab.Cmd + Shift + L打开库,然后选择颜色选项卡。

Then drag that color to the code.然后将该颜色拖到代码中。 Something like this.像这样的东西。 在此处输入图片说明

This is, assuming that you have added a color in your .xcassets And want to use that color.这是,假设您在.xcassets添加了一种颜色并且想要使用该颜色。

If you want to use the color in xibs, it is easily available in the Named Colors section, if you tap on color.如果你想在 xibs 中使用颜色,如果你点击颜色,它可以在命名颜色部分轻松获得。

You can achieve this with following easy 4 steps:您可以通过以下简单的 4 个步骤实现此目的:

  1. Adding colors to an asset catalog : Go to Assets folder in your Project -> Tap on Plus button -> After selecting your .xcassets directory in Xcode, press the plus button on the bottom left and select “New Color Set”.向资产目录添加颜色:转到项目中的资产文件夹 -> 点击加号按钮 -> 在 Xcode 中选择 .xcassets 目录后,按左下角的加号按钮并选择“新颜色集”。 Here's the attached link(GIF) it will help you about adding ColorSet.这是附加的链接(GIF),它将帮助您添加 ColorSet。

  2. Using asset catalog colors in Storyboards : In order to user asset catalog colors in Storyboard or an Interface Builder file is pretty straightforward.在 Storyboards 中使用资产目录颜色:为了在 Storyboard 或 Interface Builder 文件中使用资产目录颜色非常简单。 Color fields, including background colors, text colors, should display the colors you defined in the asset catalog under the “Named Colors” section.颜色字段,包括背景颜色、文本颜色,应显示您在“命名颜色”部分下的资产目录中定义的颜色。 Here's the attached link(GIF) it will help you about using ColorSet from Storyboard.这是附加的链接(GIF),它将帮助您使用 Storyboard 中的 ColorSet。

  3. Using asset catalog colors in CODE : Create an Extension first.在 CODE 中使用资产目录颜色:首先创建一个扩展。

You first need to create an Extension of Custom font color(I had added code below).您首先需要创建自定义字体颜色的扩展(我在下面添加了代码)。

extension UIColor {
class var appBG: UIColor {
    if let color = UIColor(named: "SillyBlue") {
        return color
    }
    fatalError("Could not find appBG color")
  }
}
  1. Then directly use in the code , example is given below:然后直接在代码中使用,示例如下:

    self.view.backgroundColor = UIColor.appBG self.view.backgroundColor = UIColor.appBG

Let me know if you face any issue.如果您遇到任何问题,请告诉我。

It should work.它应该工作。

UIColor(named:) requires iOS 11 and newer versions. UIColor(named:) 需要 iOS 11 和更新版本。

First of all, check the name of your asset.首先,检查您的资产名称。

颜色名称

After that, you should check the name in the code:之后,您应该检查代码中的名称:

someView.backgroudColor = UIColor(named: "CustomColor")

UIColor(named: "name") is only available in iOS 11 and forward as you read the documentation for it: UIColor(named: "name")仅在 iOS 11 中可用,并在您阅读其文档时向前提供:

@interface UIColor (UIColorNamedColors)
+ (nullable UIColor *)colorNamed:(NSString *)name NS_AVAILABLE_IOS(11_0);      // load from main bundle
+ (nullable UIColor *)colorNamed:(NSString *)name inBundle:(nullable NSBundle *)bundle compatibleWithTraitCollection:(nullable UITraitCollection *)traitCollection NS_AVAILABLE_IOS(11_0);
@end

In swift:迅速:

extension UIColor {

    @available(iOS 11.0, *)
    public /*not inherited*/ init?(named name: String) // load from main bundle

    @available(iOS 11.0, *)
    public /*not inherited*/ init?(named name: String, in bundle: Bundle?, compatibleWith traitCollection: UITraitCollection?)
}

So what i found is that a space was causing the issue with the colour not being referenced, UIColor (named:"bottomNavigation") .所以我发现是一个空间导致了颜色未被引用的问题, UIColor (named:"bottomNavigation") It might not give an error but there shouldn't be any space between UIColor and the bracket: UIColor(named:"bottomNavigation")它可能不会给出错误,但 UIColor 和括号之间不应该有任何空格: UIColor(named:"bottomNavigation")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM