If you are using Xcode 11 or later:
- Click
File
Swift Packages
Add Package Dependency...
- Enter: https://github.com/nbasham/BlackLabsSwiftUIColor/
Works with UIColor
and Color
. Color
is determined at runtime. This implementation grabs the current .light/.dark color and saves it, the color being saved will loose the .light/.dark color not currently in use. UIColor
uses UserDefaults.standard.uicolor(forKey)
and UserDefaults.standard.uicolors(forKey)
API.
let color = Color.red
UserDefaults.standard.set(color, forKey: "key")
if let savedColor: Color = UserDefaults.standard.color(forKey: "key") {
print("Loaded Color from UserDefaults: \(savedColor.hex)")
}
let colors: [Color] = [.red, .orange, .pink]
UserDefaults.standard.set(colors, forKey: "arraykey")
if let savedColors: [Color] = UserDefaults.standard.colors(forKey: "arraykey") {
print("Loaded [Color] from UserDefaults: \(savedColors.map { $0.hexWithAlpha })")
}