简体   繁体   中英

SwiftUI: Using Color Set from Asset Catalog

In SwiftUI, we can get a color from a color set in an asset catalog using:

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? For example, this is how it would be done in 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

在此处输入图像描述

2. In your code, find a place to define your color as a variable, in my case it'll be something like this:

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

3. Finish

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.

If not, how are dynamic colors programmatically created in SwiftUI? For example, this is how it would be done in 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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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