如何将HEX RGB颜色代码转换为UIColor?

ope*_*rog 65 iphone uicolor

我有一个RGB十六进制代码,如#ffffff作为NSString,并希望将其转换为UIColor.有一个简单的方法吗?

Dav*_*ong 102

我的一些代码中,我使用了两个不同的函数:

void SKScanHexColor(NSString * hexString, float * red, float * green, float * blue, float * alpha) {
  NSString *cleanString = [hexString stringByReplacingOccurrencesOfString:@"#" withString:@""];
  if([cleanString length] == 3) {
      cleanString = [NSString stringWithFormat:@"%@%@%@%@%@%@", 
                     [cleanString substringWithRange:NSMakeRange(0, 1)],[cleanString substringWithRange:NSMakeRange(0, 1)],
                     [cleanString substringWithRange:NSMakeRange(1, 1)],[cleanString substringWithRange:NSMakeRange(1, 1)],
                     [cleanString substringWithRange:NSMakeRange(2, 1)],[cleanString substringWithRange:NSMakeRange(2, 1)]];
  }
  if([cleanString length] == 6) {
      cleanString = [cleanString stringByAppendingString:@"ff"];
  }

  unsigned int baseValue;
  [[NSScanner scannerWithString:cleanString] scanHexInt:&baseValue];

  if (red) { *red = ((baseValue >> 24) & 0xFF)/255.0f; }
  if (green) { *green = ((baseValue >> 16) & 0xFF)/255.0f; }
  if (blue) { *blue = ((baseValue >> 8) & 0xFF)/255.0f; }
  if (alpha) { *alpha = ((baseValue >> 0) & 0xFF)/255.0f; }
}
Run Code Online (Sandbox Code Playgroud)

然后我像这样使用它:

UIColor * SKColorFromHexString(NSString * hexString) {
  float red, green, blue, alpha;
  SKScanHexColor(hexString, &red, &green, &blue, &alpha);

  return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
}
Run Code Online (Sandbox Code Playgroud)

如果您更喜欢将其用作UIColor类别,那么只需更改几行:

+ (UIColor *) colorFromHexString:(NSString *)hexString {
  NSString *cleanString = [hexString stringByReplacingOccurrencesOfString:@"#" withString:@""];
  if([cleanString length] == 3) {
      cleanString = [NSString stringWithFormat:@"%@%@%@%@%@%@", 
                     [cleanString substringWithRange:NSMakeRange(0, 1)],[cleanString substringWithRange:NSMakeRange(0, 1)],
                     [cleanString substringWithRange:NSMakeRange(1, 1)],[cleanString substringWithRange:NSMakeRange(1, 1)],
                     [cleanString substringWithRange:NSMakeRange(2, 1)],[cleanString substringWithRange:NSMakeRange(2, 1)]];
  }
  if([cleanString length] == 6) {
      cleanString = [cleanString stringByAppendingString:@"ff"];
  }

  unsigned int baseValue;
  [[NSScanner scannerWithString:cleanString] scanHexInt:&baseValue];

  float red = ((baseValue >> 24) & 0xFF)/255.0f;
  float green = ((baseValue >> 16) & 0xFF)/255.0f;
  float blue = ((baseValue >> 8) & 0xFF)/255.0f;
  float alpha = ((baseValue >> 0) & 0xFF)/255.0f;

  return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
}
Run Code Online (Sandbox Code Playgroud)

这将处理"#abc","#abcdef31"等字符串.

  • 我理解一切:`((baseValue >> 24)&0xFF)`.(我得到了255.0f的除法).你能解释一下你是如何转移24,16和8位的吗?而且,你为什么要用0xFF?谢谢 :) (2认同)

kth*_*rat 84

如果您使用十六进制值..

#define UIColorFromRGB(rgbValue) [UIColor \
       colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
       green:((float)((rgbValue & 0xFF00) >> 8))/255.0 \
       blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

  //Then use any Hex value

 self.view.backgroundColor = UIColorFromRGB(0xD2691E);   
Run Code Online (Sandbox Code Playgroud)


小智 35

我一直在寻找一个简单的解决方案,并想出了这个(不完全是Objective-C,但就像魅力一样):

NSString *stringColor = @"#AABBCC";
NSUInteger red, green, blue;
sscanf([stringColor UTF8String], "#%02X%02X%02X", &red, &green, &blue);

UIColor *color = [UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:1];
Run Code Online (Sandbox Code Playgroud)


Jim*_*des 19

UIColor有一个很好的类别叫做"UIColor + Expanded",它有一个从RGB十六进制字符串中获取UIColor的类方法:

它使用简单:

UIColor *myColor = [UIColor colorWithHexString:@"FF0000"];
Run Code Online (Sandbox Code Playgroud)

此外,它还为UIColor添加了许多其他可能有用的实用程序.本文提供了更多信息.


Mik*_*der 10

很简单,只需访问此网站并输入您的十六进制值:http://www.corecoding.com/utilities/rgb-or-hex-to-float.php


Chi*_*buZ 7

+ (UIColor *)colorWithHexString:(NSString *)colorString
{
    colorString = [colorString stringByReplacingOccurrencesOfString:@"#" withString:@""];

    if (colorString.length == 3)
        colorString = [NSString stringWithFormat:@"%c%c%c%c%c%c",
        [colorString characterAtIndex:0], [colorString characterAtIndex:0],
        [colorString characterAtIndex:1], [colorString characterAtIndex:1],
        [colorString characterAtIndex:2], [colorString characterAtIndex:2]];

    if (colorString.length == 6)
    {
        int r, g, b;
        sscanf([colorString UTF8String], "%2x%2x%2x", &r, &g, &b);
        return [UIColor colorWithRed:(r/255.0) green:(g/255.0) blue:(b/255.0) alpha:1.0];
    }
    return nil;
}
Run Code Online (Sandbox Code Playgroud)

格式#123,123,#fff195,fff195

+ (UIColor *)colorWithHexValue:(int)hexValue
{
    float red   = ((hexValue & 0xFF0000) >> 16)/255.0;
    float green = ((hexValue & 0xFF00) >> 8)/255.0;
    float blue  = (hexValue & 0xFF)/255.0;
    return [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
}
Run Code Online (Sandbox Code Playgroud)

格式为0xfff195


Ehs*_*san 5

我找到的最简单的方法:Hex到UIColor转换器

只需输入不带"#"的十六进制数字,它就会返回UIColor代码.例如,橙色(#f77f00)的代码是:

[UIColor colorWithRed:0.969 green:0.498 blue:0 alpha:1.0]
Run Code Online (Sandbox Code Playgroud)