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)];
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?
Thx for answers, guys!
UIColor
uses a range from 0 to 1.0, not integers to 255.. Try this:Please try the code
like
UIColor's RGB components are scaled between 0 and 1, not up to 255.
Your values are incorrect, you need to divide each color value by 255.0.
The docs state:
Parameters
red The red component of the color object, specified as a value from 0.0 to 1.0.
green The green component of the color object, specified as a value from 0.0 to 1.0.
blue The blue component of the color object, specified as a value from 0.0 to 1.0.
alpha The opacity value of the color object, specified as a value from 0.0 to 1.0.
Reference here.
Since @Jaswanth Kumar asked, here's the
Swift
version from LSwift:Usage:
let color = UIColor(rgb: 0x112233)
One of my favourite macros, no project without:
Using like: