95

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))
Mojtaba Hosseini
  • 95,414
  • 31
  • 268
  • 278
nOk
  • 2,725
  • 4
  • 13
  • 26
  • I know I can use the single properties but I thought there might be a simpler method – nOk Jul 11 '19 at 17:53
  • 2
    I get that but still apple might have provided me with a method already so I thought it would be legit to ask. I'm sorry if you don't think so – nOk Jul 14 '19 at 18:31
  • 1
    I since found out that there is not a built-in coercion which is why I accepted the answer to help others with the same question. – nOk Jul 14 '19 at 19:20

6 Answers6

161

Starting with beta 5, you can create a Color from a UIColor:

Color(UIColor.systemBlue)
kontiki
  • 37,663
  • 13
  • 111
  • 125
54

Both iOS and macOS

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` */

suColor vs uiColor

Note that:

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


Extension

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

Mojtaba Hosseini
  • 95,414
  • 31
  • 268
  • 278
  • 7
    This is honestly the best answer here. An extension on UIColor to return an `suColor` is brilliant. – Sam Spencer Mar 07 '20 at 20:20
  • 1
    SwiftUI's 'Color.red' is equal to UIKit's 'Color(.systemRed)' or 'UIColor.systemRed' ! This means that SwiftUI implicitly uses systemRed as default for his 'Color.red' , This is nice to know! – multitudes Aug 22 '20 at 10:30
9

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)
3

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)
}
Joannes
  • 2,569
  • 3
  • 17
  • 39
1

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.

Screenshot (27k)

   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)
AB Murphy
  • 51
  • 4
0

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)
    }
}
Harshil Patel
  • 1,318
  • 1
  • 7
  • 26