This Pod adds iOS 9+ compatibility for named colors in asset catalogs.
It extends UIColor with a new optional initializer: UIColor(asset:)
that works in the same way as UIColor(named:)
.
This will only work in code. Named colors set in Interface Builder will still not work on iOS 9 or 10!
To run the example project, clone the repo, and run pod install
from the Example directory first.
- iOS 9+
- Xcode 9+
- Swift 4
- Add
pod "ColorAssetCatalog"
to your Podfile - Run
pod install
- Add an Asset Catalog to your project called Colors.xcassets
- Add a New Copy Files Phase to your target that copies Colors.xcassets to the Resources destination (leave Subpath blank)
- Copying
Colors.xcassets
to your app's Resources directory bypasses Apple's optimisations around Asset Catalogs. Think carefully whether this solution is right for you. - You can name your
.xcassets
file something else: declareColorAssetManager.shared.catalogName = "OtherName"
before usingUIColor(asset:)
. - You can set the Bundle where your asset catalog resides: set
ColorAssetManager.shared.bundle
before usingUIColor(asset:)
or useUIColor(asset:in:compatibleWith:)
. - On iOS 11+,
UIColor(asset:)
just calls the nativeUIColor(named:)
. - By default, colors are lazily loaded to avoid re-parsing the asset JSON on subsequent use. Cached colors are released if the app receives a memory warning.
- You can disable in-memory caching of colors using
ColorAssetManager.shared.cachingEnabled = false
. - Device-specific colors are supported except Watch and Mac. You can toggle them for the Color Set in the Inspector.
- P3 (wide gamut) specific colors are supported on iOS 10+. You can toggle them for the Color Set in the Inspector.
Sam Rayner, http://www.samrayner.com
ColorAssetCatalog is available under the MIT license. See the LICENSE file for more info.