How can I create a UIColor from a hex string?

后端 未结 30 1563
北恋
北恋 2020-11-22 16:53

How can I create a UIColor from a hexadecimal string format, such as #00FF00?

30条回答
  •  温柔的废话
    2020-11-22 17:35

    This is another alternative.

    - (UIColor *)colorWithRGBHex:(UInt32)hex
    {
        int r = (hex >> 16) & 0xFF;
        int g = (hex >> 8) & 0xFF;
        int b = (hex) & 0xFF;
    
        return [UIColor colorWithRed:r / 255.0f
                               green:g / 255.0f
                                blue:b / 255.0f
                               alpha:1.0f];
    }
    

提交回复
热议问题