Skip to content

Instantly share code, notes, and snippets.

@aronbudinszky
Last active March 6, 2020 15:32
Show Gist options
  • Save aronbudinszky/2d84f8a62ec09673f54ae7b4a78617b0 to your computer and use it in GitHub Desktop.
Save aronbudinszky/2d84f8a62ec09673f54ae7b4a78617b0 to your computer and use it in GitHub Desktop.
Support named assets via enum values (to avoid string literals).
import Foundation
import SwiftUI
/// Extend Color to support named assets
extension Color {
enum Assets: String, RawRepresentable {
case appTextColor
/// Place all your color assets here...the enum case should equal to the name you use in your Asset library
}
init(asset: Color.Assets) {
self.init(asset.rawValue)
}
}
/// Extend UIColor to support named assets
extension UIColor {
convenience init(asset: Color.Assets) {
// Force unwrap okay because we are sure that Color.Assets are available
self.init(named: asset.rawValue)!
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment