相关疑难解决方法(0)

在SwiftUI中使用十六进制颜色

在UIKit中,我们可以使用扩展将十六进制颜色设置为几乎所有内容。 https://www.hackingwithswift.com/example-code/uicolor/how-to-convert-a-hex-color-to-a-uicolor

但是当我尝试在SwiftUI上执行此操作时,这是不可能的,看起来SwiftUI没有将UIColor作为参数。

    Text(text)
        .color(UIColor.init(hex: "FFF"))
Run Code Online (Sandbox Code Playgroud)

错误信息:

Cannot convert value of type 'UIColor' to expected argument type 'Color?'
Run Code Online (Sandbox Code Playgroud)

我什至尝试为Color而不是进行扩展UIColor,但我没有任何运气

我对颜色的扩展:

导入SwiftUI

extension Color {
    init(hex: String) {
        let scanner = Scanner(string: hex)
        scanner.scanLocation = 0
        var rgbValue: UInt64 = 0
        scanner.scanHexInt64(&rgbValue)

        let r = (rgbValue & 0xff0000) >> 16
        let g = (rgbValue & 0xff00) >> 8
        let b = rgbValue & 0xff

        self.init(
            red: CGFloat(r) / 0xff,
            green: CGFloat(g) / 0xff,
            blue: CGFloat(b) / 0xff, …
Run Code Online (Sandbox Code Playgroud)

uicolor ios swiftui

9
推荐指数
5
解决办法
2012
查看次数

How to convert `UIColor` to SwiftUI‘s `Color`

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))
Run Code Online (Sandbox Code Playgroud)

colors uicolor swift swiftui

6
推荐指数
3
解决办法
2065
查看次数

如何从Color SwiftUI获取RGB组件

如果我有一个SwiftUI Color

let col: Color = Color(red: 0.5, green: 0.5, blue: 0.5)
Run Code Online (Sandbox Code Playgroud)

如何从中获取RGB分量col
像这样:

print(col.components.red)
Run Code Online (Sandbox Code Playgroud)

在UIKit中,我可以使用,UIColor.getRed但SwiftUI中似乎没有等效项。

rgb colors swift swiftui

5
推荐指数
3
解决办法
822
查看次数

标签 统计

swiftui ×3

colors ×2

swift ×2

uicolor ×2

ios ×1

rgb ×1