【问题标题】:How to convert RGB to HTML color?如何将 RGB 转换为 HTML 颜色?
【发布时间】:2011-08-25 12:57:51
【问题描述】:

请告诉我如何将 RGB UIColor 转换为十六进制 HTML 颜色代码字符串?

【问题讨论】:

    标签: ios objective-c hex uicolor


    【解决方案1】:
    - (NSString *)getHexStringForColor:(UIColor*)color {
        const CGFloat *components = CGColorGetComponents(color.CGColor);
        CGFloat r = components[0];
        CGFloat g = components[1];
        CGFloat b = components[2];
    
        return [NSString stringWithFormat:@"%02X%02X%02X", (int)(r * 255), (int)(g * 255), (int)(b * 255)];
    }
    

    【讨论】:

    • 不幸的是,这不适用于像 [UIColor blackColor] 这样的非 RGB 颜色。问题只是关于 RGB 颜色,但如果你想支持更多 UIColors 你可以在这里使用 ngb 的方法:stackoverflow.com/a/13134326/211292
    猜你喜欢
    • 2011-07-17
    • 1970-01-01
    • 2010-09-26
    • 1970-01-01
    • 2019-02-08
    • 2012-02-06
    • 2017-03-21
    • 2011-05-05
    • 2013-12-10
    相关资源
    最近更新 更多