samrayner / colorassetcatalog Goto Github PK
View Code? Open in Web Editor NEWA CocoaPod that adds iOS 9+ compatibility for named colors in asset catalogs.
License: MIT License
A CocoaPod that adds iOS 9+ compatibility for named colors in asset catalogs.
License: MIT License
Hi! I've using ColorAssetCatalog without any issue until I moved Colors.xcassets into a framework I've been working on: it looks like both the iOS11 asset color loader and ColorAssetCatalog are loading resources from the main bundle.
So, when you try to load an asset color by name UIColor(asset: "StatusOnline")
the constructor returns nil.
One of the system UIColor constructors allows to load an asset color from a different bundle:
public init?(named name: String, in bundle: Bundle?, compatibleWith traitCollection: UITraitCollection?)
I followed the instructions but the loaded color is always white.
I've inserted a breakpoint after loading the color and the loaded object prints:
kCGColorSpaceModelRGB 1 1 1 1
When I define the Color Asset, do I need to take the color space or any other property in account?
This only happens in ios 9 or 10.
It´s possible to use in the InterfaceBuilder?!
ColorAssetManager needs to be public, otherwise there's no way to do this:
ColorAssetManager.shared.catalogName = "OtherName"
Impossible using colors as attributes in attributed strings with UILabel (iOS 10).
Example:
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor(asset: "baseColor")]
Got error:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UILabel: the use of CGColor for color properties or inside attributed strings is not supported.'
I've got a set of colours in the file like this one:
{
"info" : {
"version" : 1,
"author" : "xcode"
},
"colors" : [
{
"idiom" : "universal",
"color" : {
"color-space" : "srgb",
"components" : {
"red" : "1.000",
"alpha" : "1.000",
"blue" : "1.000",
"green" : "1.000"
}
}
}
]
}
When tracing through the method I can see that all is going well until it tries to JSON decode the file.
Are you able to see anything wrong with the above file that would give a reason as to why it's not working?
When I catch the JSONDecode error, this is what it shows:
typeMismatch(Swift.Double, Swift.DecodingError.Context(codingPath: [ColorAssetCatalog.ColorAssetCatalog.(CodingKeys in _DAE07DEB4F1D47BCAEDAF908209E6C48).colors, Foundation.(_JSONKey in _12768CA107A31EF2DCE034FD75B541C9)(stringValue: "Index 0", intValue: Optional(0)), ColorAssetCatalog.ColorAsset.(CodingKeys in _DAE07DEB4F1D47BCAEDAF908209E6C48).color, ColorAssetCatalog.ColorAsset.Color.(CodingKeys in _DAE07DEB4F1D47BCAEDAF908209E6C48).components, ColorAssetCatalog.ColorAsset.Color.Components.(CodingKeys in _DAE07DEB4F1D47BCAEDAF908209E6C48).alpha], debugDescription: "Expected to decode Double but found a string/data instead.", underlyingError: nil))
I bumped into an issue where colors highly saturated when using 8-bit Hexadecimal format. From what it looks, the following color:
"color" : {
"color-space" : "srgb",
"components" : {
"red" : "0x00",
"alpha" : "1.000",
"blue" : "0xD0",
"green" : "0xA0"
}
}
turns into (r: 0, g: 1, b: 1). It looks like that hexadecimal numbers are not supported. I'm planning on doing a pull request tonight, hopefully you're okay to merge it.
Hi,
Just wanted to report that the backward compatibility feature was sadly broken by recent versions of xcode. The *.xcassets
folder are now compiled into a single assets.car file that is not openable.
So the following code in ColorAssetManager
does not work anymore:
func asset(named name: String) -> ColorAsset? {
guard let dir = (bundle ?? Bundle.main).resourceURL?.appendingPathComponent("\(catalogName).xcassets"),
let files = try? FileManager.default.contentsOfDirectory(at: dir, includingPropertiesForKeys: nil, options: []),
let colorset = files.first(where: { $0.lastPathComponent == "\(name).colorset" }),
let data = try? Data(contentsOf: colorset.appendingPathComponent("Contents.json")),
let catalog = try? JSONDecoder().decode(ColorAssetCatalog.self, from: data)
else {
return nil
}
The problematic line is :
let files = try? FileManager.default.contentsOfDirectory(at: dir, includingPropertiesForKeys: nil, options: [])
Because the folder does not exists
I tried to find a way to extract the colors from the .car
file without success.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.