如何选择十六进制格式或RGB格式的颜色,而不是使用Swift中给我的颜色 [英] How do I pick the color in hexadecimal form or RGB form instead of using the colors given to me in Swift

查看:139
本文介绍了如何选择十六进制格式或RGB格式的颜色,而不是使用Swift中给我的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将TopScoreContainer的背景颜色更改为更浅的绿色。我不想使用greenColor()。以下是代码行:

I am trying to change the background color of TopScoreContainer to a lighter shade of green. I do not want to use greenColor() . Here is the line of code:

self.TopScoreContainer.backgroundColor = UIColor.greenColor()    

是否可以用十六进制数字或RGB值代替greenColor()?谢谢。

Is it possible to substitute in a hexadecimal number or RGB value instead of greenColor() ? Thanks.

推荐答案

    let myCustomColorHSBa = UIColor(hue: 120/360, saturation: 0.25 , brightness: 1.0 , alpha: 1)
    let myCustomColorRGBa = UIColor(red: 191/255, green: 1, blue: 191/255, alpha: 1)

使用它作为扩展只读计算var:

using it as an extension read-only computed var:


只读计算属性

Read-Only Computed Properties

带有getter但没有setter的计算属性称为
只读计算属性。一个只读的计算属性总是
返回一个值,可以通过点语法访问,但不能将
设置为不同的值。

A computed property with a getter but no setter is known as a read-only computed property. A read-only computed property always returns a value, and can be accessed through dot syntax, but cannot be set to a different value.

注意

您必须使用var关键字声明计算属性(包括只读计算的
属性)作为变量属性,因为它们的
值是不固定。 let关键字仅用于常量
属性,表示一旦将
设置为实例初始化的一部分,就无法更改它们的值。

You must declare computed properties—including read-only computed properties—as variable properties with the var keyword, because their value is not fixed. The let keyword is only used for constant properties, to indicate that their values cannot be changed once they are set as part of instance initialization.

您可以通过
删除get关键字及其大括号来简化只读计算属性的声明:

You can simplify the declaration of a read-only computed property by removing the get keyword and its braces:



extension UIColor {
    var lightGreen: UIColor {
        return UIColor(red: 191/255, green: 1, blue: 191/255, alpha: 1)
    }
}
let lightGreen = UIColor().lightGreen

或者你也可以创建自己的htmlColor输入,如下所示:

or you can also create your own htmlColor input as follow:

更新: Xcode 7.2•Swift 2.1.1

extension String {
    subscript(range: Range<Int>) -> String {
        return range.startIndex < 0 || range.endIndex > characters.count ? "Out of Range" : substringWithRange(Range(start: startIndex.advancedBy(range.startIndex),end: startIndex.advancedBy(range.endIndex)))
    }
    var hexaCGFloat: CGFloat {
        return CGFloat(strtoul(self, nil, 16))
    }
}

extension UIColor {
    convenience init(htmlColor: String, alpha: Double) {
        self.init(red: htmlColor[1...2].hexaCGFloat / 255.0, green: htmlColor[3...4].hexaCGFloat / 255.0, blue: htmlColor[5...6].hexaCGFloat / 255.0, alpha: CGFloat(alpha)  )
    }
    convenience init(r: Int, g:Int , b:Int , a: Int) {
        self.init(red: CGFloat(r)/255, green: CGFloat(g)/255, blue: CGFloat(b)/255, alpha: CGFloat(a)/255)
    }
}
let myColor = UIColor(r: 255 , g: 0, b: 0, a: 255)
let myHtmlWebColor = UIColor(htmlColor: "#bfffbf", alpha: 1.0)

这篇关于如何选择十六进制格式或RGB格式的颜色,而不是使用Swift中给我的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆