Menu

Asset Colors in Packages - Forest and the Trees

Forest and the Trees·Doug Marttila·7 months ago
#a9jmfq5r
#comment#package#swift#xcode#respond
Reading 0:00
15s threshold

Xcode allows you to easily add custom colors to the Assets file in your project. Add a Color Set, then use the Inspector to modify the color.

To use your new color in your project, adding a color extension with the custom color’s name makes your life easier. Example below…

extension Color { static let myAwesomeClr = Color("theNameOfMyColorInAssets") }

Then you can do things like…

Color(.myAwesomeClr)

If you want to include that color in a Package, you need to..

Declare your extension public (like everything in a Package)

Create a UIColor and cast that as a Color. (This took me a while to figure out which is why I am posting this.) See below…

public extension Color { static let myAwesomeClr = Color(UIColor(named: "theNameOfMyColorInAssets", in: .module, compatibleWith: nil)!) }

And, you need to add a resource target to your Package file…

let package = Package( name: "YourPackageName", ...…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.