How do I create a UIColor from RGBA?

前端 未结 5 1553
迷失自我
迷失自我 2020-12-23 21:39

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

5条回答
  •  一生所求
    2020-12-23 21:59

    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];
    

    The docs state:

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

    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.

提交回复
热议问题