How can I change a SwiftUI Color to UIColor?

后端 未结 5 985
醉酒成梦
醉酒成梦 2021-01-07 17:02

I\'m trying to change a SwiftUI Color to an instance of UIColor.

I can easily get the RGBA from the UIColor, but I don\'t know how to get the "Color" instan

5条回答
  •  Happy的楠姐
    2021-01-07 17:09

    iOS 14 / macOS 10.16

    There is a new initializer that takes a Color and returns a UIColor for iOS or NSColor for macOS now. So:

    iOS

    UIColor(Color.red)
    

    macOS

    NSColor(Color.red)
    

    Core Graphics

    UIColor(Color.red).cgColor /* For iOS */
    NSColor(Color.red).cgColor /* For macOS */
    

    If you are looking for color components, you can find my helpful extensions here in this answer

提交回复
热议问题