我有一个RGB十六进制代码,如#ffffff作为NSString,并希望将其转换为UIColor。有没有一种简单的方法可以做到这一点?
答案 0 :(得分:102)
In some code of mine,我使用了两种不同的功能:
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”等字符串
答案 1 :(得分: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);
答案 2 :(得分:34)
我一直在寻找一个简单的解决方案,并想出了这个(不完全是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];
答案 3 :(得分:19)
UIColor有一个很好的类别叫"UIColor+Expanded",它有一个类方法,用于从RGB十六进制字符串中获取UIColor:
使用简单:
UIColor *myColor = [UIColor colorWithHexString:@"FF0000"];
另外,它为UIColor添加了许多其他可能有用的实用程序。有关详细信息,请参阅this article。
答案 4 :(得分:10)
简单,只需访问此网站并输入您的十六进制值:http://www.corecoding.com/utilities/rgb-or-hex-to-float.php
答案 5 :(得分: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;
}
格式#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
答案 6 :(得分:5)
我找到的最简单方法:Hex to UIColor Converter
只需输入不带“#”的十六进制数字,它就会返回UIColor代码。例如,橙色(#f77f00)的代码是:
[UIColor colorWithRed:0.969 green:0.498 blue:0 alpha:1.0]
答案 7 :(得分:3)
我想我将六个字符分成三对,然后将其转换为十进制,然后将其除以255,以使每个颜色分量成为浮点数。
然后,您可以将组件传递给:
[UIColor colorWithRed: green: blue: alpha:1];
答案 8 :(得分:3)
我创建了一个在线工具,可以立即将任何十六进制代码转换为Swift和Objective-C的UIColor代码段,以便在使用自定义方法或插件时不方便:https://iosref.com/uihex/
答案 9 :(得分:2)
如果您不想编写上述所有代码,可以查看以下网站:http://www.diovo.com/apps/rgb-to-uicolor-converter.html
从像这样的HEX颜色:#FFFFFF,网站将其转换为如下字符串:
UIColor *aColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:1.000];
答案 10 :(得分:2)
如果从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
}
答案 11 :(得分:1)
不要忘记您可以选择convert your hex values to RGB并在界面构建器中输入它们。它会保存一些代码行。
答案 12 :(得分:1)
猜猜这里有点晚了......但我在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;
}
答案 13 :(得分:0)
我最终为UIColor
创建了一个类别,我可以在其他项目中重复使用。
<强>目标C 强>
#import <UIKit/UIKit.h>
@interface UIColor (HexColor)
+ (UIColor *)colorFromHex:(unsigned long)hex;
@end
@implementation UIColor (HexColor)
+ (UIColor *)colorFromHex:(unsigned long)hex
{
return [UIColor colorWithRed:((float)((hex & 0xFF0000) >> 16))/255.0 green:((float)((hex & 0xFF00) >> 8))/255.0 blue:((float)(hex & 0xFF))/255.0 alpha:1.0];
}
@end
// USAGE
UIColor *customRedColor = [UIColor colorFromHex:0x990000];
<强>夫特强>
extension UIColor {
convenience init(hex: Int) {
let red = CGFloat((hex & 0xff0000) >> 16) / 255.0
let green = CGFloat((hex & 0x00ff00) >> 8) / 255.0
let blue = CGFloat(hex & 0x0000ff) / 255.0
self.init(red: red, green: green, blue: blue, alpha: 1.0)
}
}
// USAGE
let customColor = UIColor(hex: 0x990000)
答案 14 :(得分:0)
我认为使用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值或您定义的十六进制值在代码中引用它。 例如......
[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,但在定义中总是可以更改。)
享受。
答案 15 :(得分:0)
我发现一个cocoapod库在使用“#RRGGBB”值创建UIColor时非常有用。
pod 'UIColor-HexRGB'
答案 16 :(得分:0)
+(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];
}