How do I create a UIColor from RGBA?

前端 未结 5 1551
迷失自我
迷失自我 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 22:01

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

提交回复
热议问题