如何从RGBA创建UIColor? [英] How do I create a UIColor from RGBA?

查看:199
本文介绍了如何从RGBA创建UIColor?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的项目中使用 NSAttributedString ,但是当我尝试设置颜色时,它不是来自标准集( redColor blackColor greenColor 等) UILabel 以白色显示这些字母。
这是我的代码行。

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.

[attributedString addAttribute:NSForegroundColorAttributeName
                         value:[UIColor colorWithRed:66
                                               green:79
                                                blue:91
                                               alpha:1]
                         range:NSMakeRange(0, attributedString.length)];

我试图用 CIColor 来制作颜色Core Image框架,但它显示了相同的结果。
我的代码应该以正确的方式更改我的代码?

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?

答案的答案,伙计们!

推荐答案

您的值不正确,需要将每个颜色值除以255.0。

Your values are incorrect, you need to divide each color value by 255.0.

[UIColor colorWithRed:66.0f/255.0f
                green:79.0f/255.0f
                 blue:91.0f/255.0f
                alpha:1.0f];

文档状态:

+ (UIColor *)colorWithRed:(CGFloat)red
                    green:(CGFloat)green
                     blue:(CGFloat)blue
                    alpha:(CGFloat)alpha

参数

红色
颜色对象的红色成分,指定为0.0到1.0之间的值。

red The red component of the color object, specified as a value from 0.0 to 1.0.

绿色
颜色对象的绿色组件,指定为0.0到1.0之间的值。

green The green component of the color object, specified as a value from 0.0 to 1.0.

蓝色
颜色对象的蓝色分量,指定为0.0到1.0之间的值。

blue The blue component of the color object, specified as a value from 0.0 to 1.0.

alpha
颜色的不透明度值object,指定为0.0到1.0之间的值。

alpha The opacity value of the color object, specified as a value from 0.0 to 1.0.

参考此处。

这篇关于如何从RGBA创建UIColor?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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