Skip to main content

How to load custom colors from an asset catalog

About 2 minSwiftSwiftUIArticle(s)bloghackingwithswift.comcrashcourseswiftswiftuixcodeappstore

How to load custom colors from an asset catalog 관련

SwiftUI by Example

Back to Home

How to load custom colors from an asset catalog | SwiftUI by Example

How to load custom colors from an asset catalog

Updated for Xcode 15

Improved in iOS 17

Creating your SwiftUI colors in an asset catalog is the easiest way to visualize them, but also the easiest way to add light and dark mode adjustments.

Helpfully, it's trivial to load an asset catalog color into a SwiftUI Color, and in fact if you're using Xcode 15 or later you can write this:

Color(.yourColorName)

This was introduced in Xcode 15 alongside iOS 17, but works just fine back in all older versions of iOS.

If you're using Xcode 14 and earlier, you need to write the name of your colors like this:

Color("YourColorName")

Important

With the newer Xcode 15 variant, the first letter of your color name is lowercased to fit in with standard Swift naming conventions, but if you're using the older stringly-typed approach you should keep the casing the same as it is in your asset catalog.

Similar solutions…
How to get custom colors and transparency with SF Symbols | SwiftUI by Example

How to get custom colors and transparency with SF Symbols
Polishing designs with fonts and colors | SwiftUI by Example

Polishing designs with fonts and colors
How to style text views with fonts, colors, line spacing, and more | SwiftUI by Example

How to style text views with fonts, colors, line spacing, and more
How to lazy load views using LazyVStack and LazyHStack | SwiftUI by Example

How to lazy load views using LazyVStack and LazyHStack
How to load a remote image from a URL | SwiftUI by Example

How to load a remote image from a URL

이찬희 (MarkiiimarK)
Never Stop Learning.