简体   繁体   English

SwiftUI:使用资产目录中的颜色集

[英]SwiftUI: Using Color Set from Asset Catalog

In SwiftUI, we can get a color from a color set in an asset catalog using:在 SwiftUI 中,我们可以使用以下命令从资产目录中的颜色集中获取颜色:

extension Color {
    static let coral = Color("coral")
}

This requires stringly-typed names and gets quite tedious with many color sets.这需要字符串类型的名称,并且对于许多颜色集变得非常乏味。 Is there another way to get color sets similar to how we use image literals to get images from an asset catalog?是否有另一种获取颜色集的方法,类似于我们使用图像文字从资产目录中获取图像的方式? Or, just something less redundant.或者,只是一些不那么多余的东西。

If not, how are dynamic colors programmatically created in SwiftUI?如果不是,如何在 SwiftUI 中以编程方式创建动态颜色? For example, this is how it would be done in UIKit:例如,这就是它在 UIKit 中的完成方式:

extension UIColor {
    static let dynamicColor = UIColor { $0.userInterfaceStyle == .dark ? .black : .white }
}

I want to share an alternative way to define dynamic colors in Asset catalog, but no need to write tedious code like我想分享一种在资产目录中定义动态颜色的替代方法,但不需要编写繁琐的代码

Color("yellow")

1. Define your color in asset catalog as usual 1. 像往常一样在资产目录中定义你的颜色

在此处输入图像描述

2. In your code, find a place to define your color as a variable, in my case it'll be something like this: 2. 在您的代码中,找到一个将您的颜色定义为变量的位置,在我的情况下,它将是这样的:

extension Color {
    static let ui = Color.UI()
    
    struct UI {
         let yellow = Color("yellow")
    }
}

3. Finish 3. 完成

Use your color like this:像这样使用你的颜色:

Text("Hello").background(Color.ui.yellow)

This only requires writing hard-coded color in your code for only 1 time.这只需要在代码中编写硬编码颜色 1 次。

If not, how are dynamic colors programmatically created in SwiftUI?如果不是,如何在 SwiftUI 中以编程方式创建动态颜色? For example, this is how it would be done in UIKit:例如,这就是它在 UIKit 中的完成方式:

this can be almost the same:这几乎是一样的:

extension Color {
    static let dynamicColor = Color(UIColor { traitCollection in
        return traitCollection.userInterfaceStyle == .dark ? .black : .white
    })
}

backup 备份

It's easiest to just add an extension to Color将扩展添加到Color是最简单的

extension Color {
    static let primary = Color("Primary")
    static let secondary = Color("Secondary")
}

And then it's easy to use然后它很容易使用

Text("My Primary Color")
     .foregroundColor(.primary)

If you need to use it as a UIColor then do the extension on UIColor and change implementation to如果您需要将其用作UIColor ,则在 UIColor 上进行扩展并将实现更改为

Text("My Primary UIColor")
     .foregroundColor(Color(uiColor: .primary))

I'm pretty sure Apple will leave this to developers.我很确定苹果会把这个留给开发者。 For example you can write custom SwiftGen (or wait just a little) template for SwiftUI colors.例如,您可以为 SwiftUI 颜色编写自定义SwiftGen (或稍等)模板。 https://github.com/SwiftGen/SwiftGen#colors https://github.com/SwiftGen/SwiftGen#colors

Here's another method:这是另一种方法:

import SwiftUI

enum AssetsColor : String
{
    case assetColorA
    case assetColorB
}

extension Color
{
    static func uiColor(_ name: AssetsColor) -> Color
    {
        return Color(name.rawValue)
    }
}

...

.backgroundColor(Color.uiColor(.assetColorA))

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

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