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",
... some common package info here omitted for this example ....
targets: [
.target(
name: "YourPackageName",
dependencies: [],
resources: [
.process("Assets.xcassets"),
]
),
.testTarget(
... more code here ....
Where Assets is the name of the media file that has your color.
One Comment
Thanks! the only tutorial that helps me with adding colors to a package