How to load custom colors from an asset catalog
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.