How do I convert a UIColor to a hexadecimal string?

后端 未结 3 599
独厮守ぢ
独厮守ぢ 2021-02-04 08:28

I have a project where I need to store the RGBA values of a UIColor in a database as an 8-character hexadecimal string. For example, [UIColor blueColor] would be @\"0000FFFF\".

3条回答
  •  误落风尘
    2021-02-04 09:05

    Get your floats converted to int values first, then format with stringWithFormat:

        int r,g,b,a;
    
        r = (int)(255.0 * rFloat);
        g = (int)(255.0 * gFloat);
        b = (int)(255.0 * bFloat);
        a = (int)(255.0 * aFloat);
    
        [NSString stringWithFormat:@"%02x%02x%02x%02x", r, g, b, a];
    

提交回复
热议问题