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

我有一个RGBhex代码像#ffffff作为NSString,并希望将其转换为UIColor。 有一个简单的方法来做到这一点?

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

 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; } } 

然后我使用它:

 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]; } 

如果你喜欢用它作为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]; } 

这将处理像“#abc”,“#abcdef31”等string

如果您使用hex值

 #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); 

我正在寻找一个简单的解决scheme,并提出了这个(不完全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]; 

UIColor有一个很好的类别叫做“UIColor + Expanded” ,它有一个用于从RGBhexstring获取UIColor的类方法:

使用起来很简单:

 UIColor *myColor = [UIColor colorWithHexString:@"FF0000"]; 

另外它为UIColor增加了许多其他潜在有用的工具。 更多信息在这篇文章中可用。

很简单,只要去这个网站,input你的hex值: http : //www.corecoding.com/utilities/rgb-or-hex-to-float.php

 + (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; } 

格式为#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]; } 

格式为0xfff195

最简单的方法,我发现: hexUIColor转换器

只需input不带“#”的hex数字,就会返回UIColor代码。 例如,橙色(#f77f00)的代码是:

 [UIColor colorWithRed:0.969 green:0.498 blue:0 alpha:1.0] 

我想我将六个字符分成三对,然后将其转换为十进制,然后除以255,以获得每个颜色组件作为一个浮点数。

然后您可以将组件传递给:

 [UIColor colorWithRed: green: blue: alpha:1]; 

如果你不想写上面的所有代码,你可以检查这个网站: http : //www.diovo.com/apps/rgb-to-uicolor-converter.html
从这样一个hex颜色:#FFFFFF,网站将其转换为像这样的string:

 UIColor *aColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:1.000]; 

不要忘记,您可以select将hex值转换为RGB ,并在界面生成器中input它们。 这将节省一些代码行。

RGB滑块

猜猜这有点迟了…但是我在WhiteHouse Github回购库中发现了这个版本,它以一种非常优雅的方式进行:

 +(UIColor *)colorFromRGBHexString:(NSString *)colorString { if(colorString.length == 7) { const char *colorUTF8String = [colorString UTF8String]; int r, g, b; sscanf(colorUTF8String, "#%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; } 

源链接到他们在github上的WHAppConfig.m

我创build了一个在线工具,可以立即将任何hex代码转换为Swift和Objective-C的UIColor代码片段,因为使用自定义方法或插件时不方便: http : //ebelinski.com/uihex/

如果从ObjectiveC转换到Swift对你来说很难,那么Swift的答案就是这样。 目前只需要string没有#,但你可以添加一个扫描仪的方法来跳过它,我相信。

 func stringToColor(stringColor: String) -> UIColor { var hexInt: UInt32 = 0 let scanner = NSScanner(string: stringColor) scanner.scanHexInt(&hexInt) let color = UIColor( red: CGFloat((hexInt & 0xFF0000) >> 16)/225, green: CGFloat((hexInt & 0xFF00) >> 8)/225, blue: CGFloat((hexInt & 0xFF))/225, alpha: 1) return color } 

我最终创build了一个UIColor类别,我可以在其他项目中重用。 Github: https : //github.com/mattquiros/UIColorHexColor

用法如下所示:

 UIColor *customRedColor = [UIColor colorFromHex:0x990000]; 

这比传递一个string并将其转换为一个数字然后移动这些比特要快得多。 您还可以从.pch文件中导入类别,以便您可以轻松地在应用程序的任何位置使用colorFromHex ,如将其内置到UIColor

 #ifdef __OBJC__ #import <UIKit/UIKit.h> #import <Foundation/Foundation.h> // Your other stuff here... #import "UIColor+HexColor.h" #endif 

我认为使用HEX值有更简单的方法。 只需在文件顶部添加一个定义或引用转换的头文件(UIColorFromRGB)。 你甚至可以添加一个固定HEX颜色值的模板。

 #define CLR_YELLOW_TEXT 0xf4dc89 // A Light Yellow text #define CLR_GREEN_TEXT 0x008040 // Dark Green text for my buttons #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] 

然后直接使用HEX值或您定义的hex值在代码中引用它。 例如…

 [myButton1 setTitleColor:UIColorFromRGB(0xd02d2d) forState:UIControlStateNormal]; [myButton2 setTitleColor:UIColorFromRGB(CLR_GREEN_TEXT) forState:UIControlStateNormal]; [myButton3 setTitleColor:UIColorFromRGB(CLR_YELLOW_TEXT) forState:UIControlStateNormal]; 

(PS – 这个假设是1.0的Alpha,但是在定义中总是可以改变的)。

请享用。

我发现使用“#RRGGBB”值创buildUIColor非常有用的cocoapod库。

 pod 'UIColor-HexRGB' 
 +(UIColor*)colorWithHexString:(NSString*)hexString 

{

 NSString *cString = [[hexString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString]; if ([cString length] < 6) return [UIColor grayColor]; if ([cString hasPrefix:@"0X"]) cString = [cString substringFromIndex:2]; if ([cString length] != 6) return [UIColor grayColor]; NSRange range; range.location = 0; range.length = 2; NSString *rString = [cString substringWithRange:range]; range.location = 2; NSString *gString = [cString substringWithRange:range]; range.location = 4; NSString *bString = [cString substringWithRange:range]; unsigned int r, g, b; [[NSScanner scannerWithString:rString] scanHexInt:&r]; [[NSScanner scannerWithString:gString] scanHexInt:&g]; [[NSScanner scannerWithString:bString] scanHexInt:&b]; return [UIColor colorWithRed:((float) r / 255.0f) green:((float) g / 255.0f) blue:((float) b / 255.0f) alpha:1.0f]; 

}