如何从UIColor或rgb获取十六进制字符串 [英] How can I get a hex string from UIColor or from rgb

查看:97
本文介绍了如何从UIColor或rgb获取十六进制字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我可以像这样将十六进制字符串转换为rgb颜色:

Now I can convert a hex string to rgb color like this:

// Input is without the # ie : white = FFFFFF
+ (UIColor *)colorWithHexString:(NSString *)hexString
{
    unsigned int hex;
    [[NSScanner scannerWithString:hexString] scanHexInt:&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];
}

bu如何将rgb转换为十六进制字符串?

bu how can I convert rgb to hex string?

推荐答案

使用此方法:

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

这篇关于如何从UIColor或rgb获取十六进制字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆