ios 如何从 RGBA 创建 UIColor?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13224206/
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 do I create a UIColor from RGBA?
提问by agoncharov
I want to use NSAttributedString
in my project, but when I'm trying to set color, which isn't from the standard set (redColor
, blackColor
, greenColor
etc.) UILabel
displays these letters in white color.
Here is my line of this code.
我想用NSAttributedString
在我的项目,但是当我试图设置颜色,这是不是从标准组(redColor
,blackColor
,greenColor
等)UILabel
显示在白色这些信件。这是我的这段代码。
[attributedString addAttribute:NSForegroundColorAttributeName
value:[UIColor colorWithRed:66
green:79
blue:91
alpha:1]
range:NSMakeRange(0, attributedString.length)];
I tried to make color with CIColor
from Core Image framework, but it showed the same results.
What should I change in my code to perform it in right way?
我试图用CIColor
Core Image 框架制作颜色,但它显示了相同的结果。我应该在我的代码中更改什么才能以正确的方式执行它?
Thx for answers, guys!
谢谢你们的答案,伙计们!
回答by yfrancis
Your values are incorrect, you need to divide each color value by 255.0.
您的值不正确,您需要将每个颜色值除以 255.0。
[UIColor colorWithRed:66.0f/255.0f
green:79.0f/255.0f
blue:91.0f/255.0f
alpha:1.0f];
The docs state:
文档状态:
+ (UIColor *)colorWithRed:(CGFloat)red
green:(CGFloat)green
blue:(CGFloat)blue
alpha:(CGFloat)alpha
Parameters
参数
redThe red component of the color object, specified as a value from 0.0 to 1.0.
red颜色对象的红色分量,指定为 0.0 到 1.0 之间的值。
greenThe green component of the color object, specified as a value from 0.0 to 1.0.
green颜色对象的绿色分量,指定为 0.0 到 1.0 之间的值。
blueThe blue component of the color object, specified as a value from 0.0 to 1.0.
blue颜色对象的蓝色分量,指定为 0.0 到 1.0 之间的值。
alphaThe opacity value of the color object, specified as a value from 0.0 to 1.0.
alpha颜色对象的不透明度值,指定为 0.0 到 1.0 之间的值。
回答by Geri Borbás
One of my favourite macros, no project without:
我最喜欢的宏之一,没有项目没有:
#define RGB(r, g, b) [UIColor colorWithRed:(float)r / 255.0 green:(float)g / 255.0 blue:(float)b / 255.0 alpha:1.0]
#define RGBA(r, g, b, a) [UIColor colorWithRed:(float)r / 255.0 green:(float)g / 255.0 blue:(float)b / 255.0 alpha:a]
Using like:
使用类似:
[attributedString addAttribute:NSForegroundColorAttributeName
value:RGB(66, 79, 91)
range:NSMakeRange(0, attributedString.length)];
回答by calimarkus
UIColor
uses a range from 0 to 1.0, not integers to 255.. Try this:
UIColor
使用从 0 到 1.0 的范围,而不是整数到 255.. 试试这个:
// create color
UIColor *color = [UIColor colorWithRed:66/255.0
green:79/255.0
blue:91/255.0
alpha:1];
// use in attributed string
[attributedString addAttribute:NSForegroundColorAttributeName
value:color
range:NSMakeRange(0, attributedString.length)];
回答by SameSung Vs Iphone
Please try the code
请尝试代码
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:77.0/255.0f green:104.0/255.0f blue:159.0/255.0f alpha:1.0] range:NSMakeRange(0, attributedString.length)];
like
喜欢
Label.textColor=[UIColor colorWithRed:77.0/255.0f green:104.0/255.0f blue:159.0/255.0f alpha:1.0];
UIColor's RGB components are scaled between 0 and 1, not up to 255.
UIColor 的 RGB 分量在 0 和 1 之间缩放,而不是 255。
回答by superarts.org
Since @Jaswanth Kumar asked, here's the Swift
version from LSwift:
既然@Jaswanth Kumar 问了,这里是LSwift的Swift
版本:
extension UIColor {
convenience init(rgb:UInt, alpha:CGFloat = 1.0) {
self.init(
red: CGFloat((rgb & 0xFF0000) >> 16) / 255.0,
green: CGFloat((rgb & 0x00FF00) >> 8) / 255.0,
blue: CGFloat(rgb & 0x0000FF) / 255.0,
alpha: CGFloat(alpha)
)
}
}
Usage: let color = UIColor(rgb: 0x112233)
用法: let color = UIColor(rgb: 0x112233)