简体   繁体   English

用自定义颜色填充 RoundedRectangle

[英]Fill a RoundedRectangle with custom color

 RoundedRectangle(cornerRadius: 8, style: .continuous)
    .foregroundColor(Color.init(red: 255, green: 245, blue: 158))

My rounded rectangle is all white.我的圆角矩形全是白色的。 Why can't I initiate a custom color?为什么我不能启动自定义颜色? Whereas Color.red works perfectly fine?而 Color.red 工作得非常好?

You can write an extension to take Int s as parameters to Color initializer.您可以编写一个扩展以将Int s 作为Color初始化程序的参数。 It will allow you to use Color(red: 255, green: 245, blue: 158) :它将允许您使用Color(red: 255, green: 245, blue: 158)

extension Color {

    public init(red: Int, green: Int, blue: Int) {
        self = Color(red: Double(red)/255.0,
                     green: Double(green)/255.0,
                     blue: Double(blue)/255.0)
    }
}
Color.init(red: CGFloat, green: CGFloat, blue: CGFloat)

takes a 3 CGFloat s with values between 0 and 1取值在 0 到 1 之间的 3 个CGFloat s

What you need is…你需要的是……

Color(red: 255/255, green: 245/255, blue: 158/255)

Note that the .init isn't required in Swift请注意,Swift 中不需要.init

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM