ios 如何在 Swift 中使用 UIColorFromRGB?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24074257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I use UIColorFromRGB in Swift?
提问by NANNAV
In Objective-C, we use this code to set RGB color codes for views:
在 Objective-C 中,我们使用此代码为视图设置 RGB 颜色代码:
#define UIColorFromRGB(rgbValue)
[UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
view.backgroundColor=UIColorFromRGB(0x209624);
How can I use this in Swift?
我如何在 Swift 中使用它?
回答by Nate Cook
Here's a Swift version of that function (for getting a UIColor representation of a UInt
value):
这是该函数的 Swift 版本(用于获取UInt
值的 UIColor 表示):
func UIColorFromRGB(rgbValue: UInt) -> UIColor {
return UIColor(
red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
alpha: CGFloat(1.0)
)
}
view.backgroundColor = UIColorFromRGB(0x209624)
回答by user3153627
I wanted to put
我想放
cell.backgroundColor = UIColor.colorWithRed(125/255.0, green: 125/255.0, blue: 125/255.0, alpha: 1.0)
but that didn't work.
但这没有用。
So I used:
For Swift
所以我使用了:
对于 Swift
cell.backgroundColor = UIColor(red: 0.5, green: 0.5, blue: 0.5, alpha: 1.0)
So this is the workaround that I found.
所以这是我找到的解决方法。
回答by bloudermilk
I really liked Nate Cook's answer but I wanted something a little more idiomatic. I believe this is a really good use case for a convenience initializer via a custom extension.
我真的很喜欢 Nate Cook 的回答,但我想要一些更地道的东西。我相信这是通过自定义扩展方便初始化程序的一个非常好的用例。
// UIColorExtensions.swift
import Foundation
import UIKit
extension UIColor {
convenience init(rgb: UInt) {
self.init(
red: CGFloat((rgb & 0xFF0000) >> 16) / 255.0,
green: CGFloat((rgb & 0x00FF00) >> 8) / 255.0,
blue: CGFloat(rgb & 0x0000FF) / 255.0,
alpha: CGFloat(1.0)
)
}
}
This can now be used like so:
现在可以这样使用:
view.backgroundColor = UIColor(rgb: 0x209624)
I would only recommend monkey patching UIKit classes like this in your own client code, not libraries.
我只建议在您自己的客户端代码中使用猴子修补 UIKit 类,而不是库。
回答by Ankit Goyal
myLabel.backgroundColor = UIColor(red: 50.0/255, green: 150.0/255, blue: 65.0/255, alpha: 1.0)
回答by puneeth
The simplest way to add color programmatically is by using ColorLiteral.
以编程方式添加颜色的最简单方法是使用ColorLiteral。
Just add the property ColorLiteral as shown in the example, Xcode will prompt you with a whole list of colors which you can choose. The advantage of doing so is lesser code, add HEX values or RGB. You will also get the recently used colors from the storyboard.
只需添加属性 ColorLiteral 如示例中所示,Xcode 将提示您完整的颜色列表供您选择。这样做的好处是代码更少,添加 HEX 值或 RGB。您还将从故事板中获取最近使用的颜色。
回答by Ethan Strider
If you're starting from a string (not hex) this is a function that takes a hex string and returns a UIColor.
(You can enter hex strings with either format: #ffffff
or ffffff
)
如果您从字符串(不是十六进制)开始,这是一个接受十六进制字符串并返回 UIColor 的函数。
(您可以使用以下任一格式输入十六进制字符串:#ffffff
或ffffff
)
Usage:
用法:
var color1 = hexStringToUIColor("#d3d3d3")
Swift 4:
斯威夫特 4:
func hexStringToUIColor (hex:String) -> UIColor {
var cString:String = hex.trimmingCharacters(in: .whitespacesAndNewlines).uppercased()
if (cString.hasPrefix("#")) {
cString.remove(at: cString.startIndex)
}
if ((cString.count) != 6) {
return UIColor.gray
}
var rgbValue:UInt32 = 0
Scanner(string: cString).scanHexInt32(&rgbValue)
return UIColor(
red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
alpha: CGFloat(1.0)
)
}
Swift 3:
斯威夫特 3:
func hexStringToUIColor (hex:String) -> UIColor {
var cString:String = hex.trimmingCharacters(in: .whitespacesAndNewlines).uppercased()
if (cString.hasPrefix("#")) {
cString.remove(at: cString.startIndex)
}
if ((cString.characters.count) != 6) {
return UIColor.gray
}
var rgbValue:UInt32 = 0
Scanner(string: cString).scanHexInt32(&rgbValue)
return UIColor(
red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
alpha: CGFloat(1.0)
)
}
Swift 2:
斯威夫特 2:
func hexStringToUIColor (hex:String) -> UIColor {
var cString:String = hex.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet() as NSCharacterSet).uppercaseString
if (cString.hasPrefix("#")) {
cString = cString.substringFromIndex(cString.startIndex.advancedBy(1))
}
if ((cString.characters.count) != 6) {
return UIColor.grayColor()
}
var rgbValue:UInt32 = 0
NSScanner(string: cString).scanHexInt(&rgbValue)
return UIColor(
red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
alpha: CGFloat(1.0)
)
}
Source: arshad/gist:de147c42d7b3063ef7bc
回答by Cons Bulaquena
For Xcode 9, use UIColor
with RGB values.
对于Xcode 9,使用UIColor
RGB 值。
shareBtn.backgroundColor = UIColor( red: CGFloat(92/255.0), green: CGFloat(203/255.0), blue: CGFloat(207/255.0), alpha: CGFloat(1.0) )
Preview:
预览:
See additional Apple documentation on UIColor.
请参阅有关 UIColor 的其他Apple 文档。
回答by Mahesh Chaudhari
UIColorFromRGB in Swift 4
Swift 4 中的 UIColorFromRGB
button.layer.backgroundColor = UIColor(red: 112.0/255, green: 86.0/255, blue: 164.0/255, alpha: 1.0).cgColor
回答by Imtee
I have used the following in swift.
我在 swift 中使用了以下内容。
let appRedColor = UIColor(red: 200.0/255.0, green: 16.0/255.0, blue: 46.0/255.0, alpha: 1.0)
let appSilverColor = UIColor(red: 236.0/255.0, green: 236.0/255.0, blue: 236.0/255.0, alpha: 1.0)
let appWhiteColor = UIColor(red: 255.0/255.0, green: 255.0/255.0, blue: 255.0/255.0, alpha: 1.0)
let appNavyColor = UIColor(red: 19.0/255.0, green: 41.0/255.0, blue: 75.0/255.0, alpha: 1.0)
回答by Vyacheslav
solution for argb format:
argb 格式的解决方案:
// UIColorExtensions.swift
import UIKit
extension UIColor {
convenience init(argb: UInt) {
self.init(
red: CGFloat((argb & 0xFF0000) >> 16) / 255.0,
green: CGFloat((argb & 0x00FF00) >> 8) / 255.0,
blue: CGFloat(argb & 0x0000FF) / 255.0,
alpha: CGFloat((argb & 0xFF000000) >> 24) / 255.0
)
}
}
usage:
用法:
var clearColor: UIColor = UIColor.init(argb: 0x00000000)
var redColor: UIColor = UIColor.init(argb: 0xFFFF0000)