我有一个像#ffffff 这样的 RGB 十六进制代码作为 NSString,并希望将其转换为 UIColor。有没有一种简单的方法可以做到这一点?
http://uicolor.io/
的 URL 已更改 http://uicolor.xyz
In some code of mine,我使用 2 个不同的功能:
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”等字符串。
如果您使用的是十六进制值..
#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);
我一直在寻找一个简单的解决方案并想出了这个(不完全是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",它有一个从 RGB 十六进制字符串中获取 UIColor 的类方法:
使用简单:
UIColor *myColor = [UIColor colorWithHexString:@"FF0000"];
此外,它还为 UIColor 添加了许多其他可能有用的实用程序。 this article 中提供了更多信息。
+ (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
我发现的最简单的方法:Hex to UIColor Converter
只需键入不带“#”的十六进制数字,它就会返回 UIColor 代码。例如橙色 (#f77f00) 的代码是:
[UIColor colorWithRed:0.969 green:0.498 blue:0 alpha:1.0]
我想我会将六个字符分成三对,然后将其转换为十进制,然后将其除以 255 以获得每个颜色分量作为浮点数。
然后,您可以将组件传递给:
[UIColor colorWithRed: green: blue: alpha:1];
如果从 ObjectiveC 转换到 swift 对你来说很难,这里是 Swift 的答案。目前它只需要没有#的字符串,但我相信你可以添加一个扫描器方法来跳过它。
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)/255,
green: CGFloat((hexInt & 0xFF00) >> 8)/255,
blue: CGFloat((hexInt & 0xFF))/255,
alpha: 1)
return color
}
我创建了一个在线工具,可以立即将任何十六进制代码转换为适用于 Swift 和 Objective-C 的 UIColor 代码片段,以便在不方便使用自定义方法或插件时使用:https://iosref.com/uihex/
如果你不想写上面的所有代码,你可以检查这个网站:http://www.diovo.com/apps/rgb-to-uicolor-converter.html
从像这样的十六进制颜色:#FFFFFF,网站将它转换成这样的字符串:
UIColor *aColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:1.000];
不要忘记您可以选择 convert your hex values to RGB 并在界面构建器中输入它们。它将节省一些代码行。
https://i.stack.imgur.com/H6KlU.png
猜猜这有点晚了......但我在 WhiteHouse Github repo 中找到了这个版本,它以一种非常优雅的方式完成了它:
+(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;
}
Source Link to their WHAppConfig.m on github
我认为使用 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]
然后直接使用十六进制值或您定义的十六进制值在您的代码中引用它。例如...
[myButton1 setTitleColor:UIColorFromRGB(0xd02d2d) forState:UIControlStateNormal];
[myButton2 setTitleColor:UIColorFromRGB(CLR_GREEN_TEXT) forState:UIControlStateNormal];
[myButton3 setTitleColor:UIColorFromRGB(CLR_YELLOW_TEXT) forState:UIControlStateNormal];
(PS - 这假定 Alpha 为 1.0,但它始终可以在定义中更改)。
享受。
我发现一个 cocoapod 库在使用“#RRGGBB”值创建 UIColor 时非常有用。
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];
}
((baseValue >> 24) & 0xFF)
。 (我得到了 255.0f 的除法)。你能解释一下你是如何实现移位 24、16 和 8 位的吗?还有,你为什么要使用 0xFF?谢谢 :)