SwiftUI(4)Color

2023-03-07  本文已影响0人  Z小新

16进制颜色使用

 RoundedRectangle(cornerRadius: 10)
            .fill(Color.init(hex: 0x87CEEB))
            .frame(width: 300, height: 200)

Color扩展方法

extension Color {
    init(hex: Int, alpha: Double = 1) {
        let components = (
            R: Double((hex >> 16) & 0xff) / 255,
            G: Double((hex >> 08) & 0xff) / 255,
            B: Double((hex >> 00) & 0xff) / 255
        )
        self.init(
            .sRGB,
            red: components.R,
            green: components.G,
            blue: components.B,
            opacity: alpha
        )
    }
}

展示:


image.png

自定义颜色

image.png

设置名称,暗黑模式色值


image.png

使用:

RoundedRectangle(cornerRadius: 10)
            .fill(Color("ColorThem"))
            .frame(width: 300, height: 200)

展示:


image.png image.png
上一篇 下一篇

猜你喜欢

热点阅读