I want to use a UIColor as foregroundcolor for an object but I don’t know how to convert UIColor to a Color
var myColor: UIColor
RoundedRectangle(cornerRadius: 5).foregroundColor(UIColor(myColor))
I want to use a UIColor as foregroundcolor for an object but I don’t know how to convert UIColor to a Color
var myColor: UIColor
RoundedRectangle(cornerRadius: 5).foregroundColor(UIColor(myColor))
Starting with beta 5, you can create a Color from a UIColor:
Color(UIColor.systemBlue)
Color
has a native initializer for it that takes an UIColor
or NSColor
as an argument:
Color(.red) /* or any other UIColor/NSColor you need INSIDE parentheses */
DO NOT call Color(UIColor.red)
explicitly !!!. This will couple your SwiftUI code to the UIKit
. Instead, just call Color(.red)
That will infer the correct module automatically.
Also, Note this difference:
Color.red /* this `red` is SwiftUI native `red` */
Color(.red) /* this `red` is UIKit `red` */
Color.red
and UIColor.red
are NOT same! They have different values and look different with each other. So DON'T assume this worth nothing
These are equal instead: SwiftUI.Color.Red == UIKit.UIColor.systemRed
Also, You can check out How to get RGB components from SwiftUI.Color
You can implement a custom variable for it to make it more like cgColor
and ciColor
extension UIColor {
/// The SwiftUI color associated with the receiver.
var suColor: Color { Color(self) }
}
so it would be like:
UIColor.red // UIKit color
UIColor.red.suColor // SwiftUI color
UIColor.red.cgColor // Core graphic color
UIColor.red.ciColor // Core image color
Note: Click here to see How to convert SwiftUI.Color
to UIColor
Using two helper extensions:
To extract components from UIColor
:
extension UIColor {
var rgba: (red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat) {
var red: CGFloat = 0
var green: CGFloat = 0
var blue: CGFloat = 0
var alpha: CGFloat = 0
getRed(&red, green: &green, blue: &blue, alpha: &alpha)
return (red, green, blue, alpha)
}
}
To init
with UIColor
:
extension Color {
init(uiColor: UIColor) {
self.init(red: Double(uiColor.rgba.red),
green: Double(uiColor.rgba.green),
blue: Double(uiColor.rgba.blue),
opacity: Double(uiColor.rgba.alpha))
}
}
Usage:
Color(uiColor: .red)
In Swift UI custom colors with a convenient extension:
extension UIColor {
struct purple {
static let normal = UIColor(red:0.043, green:0.576 ,blue:0.588 , alpha:1.00)
static let light = UIColor(red: 1, green: 1, blue: 1, alpha: 1)
static let dark = UIColor(red: 1, green: 1, blue: 1, alpha: 1)
}
struct gray {
static let normal = UIColor(red:0.5, green:0.5 ,blue:0.5 , alpha:1.00)
static let dark = UIColor(red: 1, green: 1, blue: 1, alpha: 1)
}
}
Wrapping Color
of SwiftUI
:
extension UIColor {
var toSUIColor: Color {
Color(self)
}
}
and using this:
var body: some View {
Text("Hello World")
.foregroundColor(Color(UIColor.purple.normal))
.background(Color(UIColor.gray.normal))
// with wrap
//.foregroundColor(UIColor.purple.normal.toSUIColor)
//.background(UIColor.gray.normal.toSUIColor)
}
I'm a really old hobbyist. Here is one way that works for me. Yes, I do use globals for reusable statements in a Constant.swift file. This example is inline so that it is easier to see. I do not say this is the way to go, it is just my old way.
import SwiftUI
// named color from the developer's pallet
let aluminumColor = Color(UIColor.lightGray)
// a custom color I use often
let butterColor = Color.init(red: 0.9993399978,
green: 0.9350042167,
blue: 0.5304131241)
// how I use this in a SwiftUI VStack:
Text("Fur People")
.font(.title)
.foregroundColor(aluminumColor)
.shadow(color: butterColor, radius: 4, x: 2, y: 2)
.minimumScaleFactor(0.75)
Create an extension like this:
extension Color {
static let someColor = Color(UIColor.systemIndigo) // << Select any UIColor
}
Usage:
struct ContentView: View {
var body: some View {
Rectangle()
.frame(width: 100, height: 100)
.foregroundColor(.someColor)
}
}