SwiftUI: Using Color Set from Asset Catalog

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

enter image description here

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.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)