I have an RGB hex code like #ffffff as NSString and want to convert that into an UIColor. Is there a simple way to do that?
我有一个RGB十六进制代码,如#ffffff作为NSString,并希望将其转换为UIColor。有没有一种简单的方法?
17 个解决方案
#1
102
In some code of mine, I use 2 different functions:
在我的一些代码中,我使用了两个不同的函数:
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; }
}
And then I use it like this:
然后我用它来表示
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];
}
If you prefer to use this as a UIColor
category, then it's just a matter of altering a few lines:
如果你喜欢把它作为一个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];
}
This will handle strings like "#abc", "#abcdef31", etc.
这将处理诸如“#abc”、“#abcdef31”等字符串。
#2
85
If you are using Hex Values..
如果您使用的是十六进制值。
#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);
#3
35
I was looking for a simple solution and came up with this (not completely Objective-C, but works like a charm):
我正在寻找一个简单的解决方案,并提出了这个(不是完全的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];
#4
20
There is a nice category for UIColor called "UIColor+Expanded" which has a class method for getting a UIColor from an RGB hex string:
UIColor有一个很好的分类,叫做“UIColor+扩展”,它有一个从RGB hex字符串获取UIColor的类方法:
It's simple to use:
这是简单的使用:
UIColor *myColor = [UIColor colorWithHexString:@"FF0000"];
Plus it adds a lot of other potentially useful utilities to UIColor. More info is available in this article.
此外,它还为UIColor添加了许多其他可能有用的实用工具。本文提供了更多的信息。
#5
10
Easy, Just go to this website and type in your hex value: http://www.corecoding.com/utilities/rgb-or-hex-to-float.php
很简单,只要进入这个网站,输入你的十六进表值:http://www.corecoding.com/applicties/rgb -or- hexto -float.php。
#6
8
+ (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;
}
for format #123, 123, #fff195, fff195
格式#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];
}
for format 0xfff195
对于格式0 xfff195
#7
6
The easiest way that I found: Hex to UIColor Converter
我找到的最简单的方法是:Hex - UIColor转换器。
Just type the hex number without '#', and it returns the UIColor code. For example the code for the orange color (#f77f00) is:
只需输入十六进制号码,而不用“#”,它返回UIColor代码。例如,橙色(#f77f00)的代码是:
[UIColor colorWithRed:0.969 green:0.498 blue:0 alpha:1.0]
#8
4
I think I'd split the six characters into three pairs, then convert that to decimal, then divide that by 255 to get each color component as a float.
我想我会把六个字符分成三组,然后把它转换成十进制,然后除以255,使每个颜色分量都变成一个浮点数。
You can then pass the components to:
然后您可以将组件传递给:
[UIColor colorWithRed: green: blue: alpha:1];
#9
3
If you don't want to write all of this code above, you can check this site: http://www.diovo.com/apps/rgb-to-uicolor-converter.html
From an HEX color like this: #FFFFFF, the site convert it in a string like this:
如果你不想写以上所有的代码,你可以检查这个网站:http://www.diovo.com/apps/rgbto -uicolor-converter.html从HEX的颜色,像这样:#FFFFFF,网站将它转换成这样的字符串:
UIColor *aColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:1.000];
#10
2
Don't forget that you have the option to convert your hex values to RGB and enter them in the interface builder. It'll save some lines of code.
不要忘记,您可以选择将十六进制值转换为RGB,并在接口构建器中输入它们。这样可以节省一些代码。
#11
2
Guess this is a bit late here... but I found this version in the WhiteHouse Github repo that does it in a pretty elegant way:
我想这里有点晚了……但是我在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
源链接到它们的WHAppConfig。米在github上
#12
2
I created an online tool to instantly convert any hex code to a UIColor code snippet for Swift and Objective-C, for when it's inconvenient to use custom methods or plugins: http://ebelinski.com/uihex/
我创建了一个在线工具,可以立即将任何十六进行代码转换为Swift和Objective-C的UIColor代码片段,因为当使用自定义方法或插件时不方便:http://ebelinski.com/uihex/。
#13
1
If converting from ObjectiveC to swift is hard for you, here's the answer with Swift. Currently it only takes strings without the #, but you can add a scanner method to skip it I believe.
如果从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)/225,
green: CGFloat((hexInt & 0xFF00) >> 8)/225,
blue: CGFloat((hexInt & 0xFF))/225,
alpha: 1)
return color
}
#14
0
I ended up creating a category for UIColor
that I can just reuse in my other projects. Github: https://github.com/mattquiros/UIColorHexColor
我最终为UIColor创建了一个类别,我可以在其他项目中重用它。Github:https://github.com/mattquiros/UIColorHexColor
The usage goes like:
使用是一样的:
UIColor *customRedColor = [UIColor colorFromHex:0x990000];
This is far faster than passing on a string and converting it to a number then shifting the bits. You can also import the category from inside your .pch
file so you can easily use colorFromHex
everywhere in your app like it's built-in to UIColor
:
这比传递一个字符串并将其转换为数字然后移动比特的速度要快得多。你也可以从你的.pch文件中导入这个类别,这样你就可以在你的应用程序中随处使用colorFromHex,比如它内置到UIColor:
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
// Your other stuff here...
#import "UIColor+HexColor.h"
#endif
#15
0
I think there is an even easier way when using HEX values. Just add a definition at the top of your file or reference a header file for the conversion (UIColorFromRGB). You can even add a template of fixed HEX color values.
我认为使用十六进制值更容易。只需在文件的顶部添加一个定义,或者引用一个用于转换的头文件(UIColorFromRGB)。您甚至可以添加固定十六进制颜色值的模板。
#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]
Then just reference it in your code using the HEX values directly or your defined hex values. For example...
然后在代码中直接引用HEX值或定义的十六进制值。例如……
[myButton1 setTitleColor:UIColorFromRGB(0xd02d2d) forState:UIControlStateNormal];
[myButton2 setTitleColor:UIColorFromRGB(CLR_GREEN_TEXT) forState:UIControlStateNormal];
[myButton3 setTitleColor:UIColorFromRGB(CLR_YELLOW_TEXT) forState:UIControlStateNormal];
(PS - This assumes an Alpha of 1.0, but it can always be changed in the definition).
(PS -这个假设是1.0的Alpha值,但是在定义中它总是可以改变的)。
Enjoy.
享受。
#16
0
I found a cocoapod library very useful in creating UIColor using "#RRGGBB" values.
我发现一个cocoapod库非常有用,它可以使用“#RRGGBB”值创建UIColor。
pod 'UIColor-HexRGB'
#17
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];
}
}
#1
102
In some code of mine, I use 2 different functions:
在我的一些代码中,我使用了两个不同的函数:
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; }
}
And then I use it like this:
然后我用它来表示
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];
}
If you prefer to use this as a UIColor
category, then it's just a matter of altering a few lines:
如果你喜欢把它作为一个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];
}
This will handle strings like "#abc", "#abcdef31", etc.
这将处理诸如“#abc”、“#abcdef31”等字符串。
#2
85
If you are using Hex Values..
如果您使用的是十六进制值。
#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);
#3
35
I was looking for a simple solution and came up with this (not completely Objective-C, but works like a charm):
我正在寻找一个简单的解决方案,并提出了这个(不是完全的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];
#4
20
There is a nice category for UIColor called "UIColor+Expanded" which has a class method for getting a UIColor from an RGB hex string:
UIColor有一个很好的分类,叫做“UIColor+扩展”,它有一个从RGB hex字符串获取UIColor的类方法:
It's simple to use:
这是简单的使用:
UIColor *myColor = [UIColor colorWithHexString:@"FF0000"];
Plus it adds a lot of other potentially useful utilities to UIColor. More info is available in this article.
此外,它还为UIColor添加了许多其他可能有用的实用工具。本文提供了更多的信息。
#5
10
Easy, Just go to this website and type in your hex value: http://www.corecoding.com/utilities/rgb-or-hex-to-float.php
很简单,只要进入这个网站,输入你的十六进表值:http://www.corecoding.com/applicties/rgb -or- hexto -float.php。
#6
8
+ (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;
}
for format #123, 123, #fff195, fff195
格式#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];
}
for format 0xfff195
对于格式0 xfff195
#7
6
The easiest way that I found: Hex to UIColor Converter
我找到的最简单的方法是:Hex - UIColor转换器。
Just type the hex number without '#', and it returns the UIColor code. For example the code for the orange color (#f77f00) is:
只需输入十六进制号码,而不用“#”,它返回UIColor代码。例如,橙色(#f77f00)的代码是:
[UIColor colorWithRed:0.969 green:0.498 blue:0 alpha:1.0]
#8
4
I think I'd split the six characters into three pairs, then convert that to decimal, then divide that by 255 to get each color component as a float.
我想我会把六个字符分成三组,然后把它转换成十进制,然后除以255,使每个颜色分量都变成一个浮点数。
You can then pass the components to:
然后您可以将组件传递给:
[UIColor colorWithRed: green: blue: alpha:1];
#9
3
If you don't want to write all of this code above, you can check this site: http://www.diovo.com/apps/rgb-to-uicolor-converter.html
From an HEX color like this: #FFFFFF, the site convert it in a string like this:
如果你不想写以上所有的代码,你可以检查这个网站:http://www.diovo.com/apps/rgbto -uicolor-converter.html从HEX的颜色,像这样:#FFFFFF,网站将它转换成这样的字符串:
UIColor *aColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:1.000];
#10
2
Don't forget that you have the option to convert your hex values to RGB and enter them in the interface builder. It'll save some lines of code.
不要忘记,您可以选择将十六进制值转换为RGB,并在接口构建器中输入它们。这样可以节省一些代码。
#11
2
Guess this is a bit late here... but I found this version in the WhiteHouse Github repo that does it in a pretty elegant way:
我想这里有点晚了……但是我在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
源链接到它们的WHAppConfig。米在github上
#12
2
I created an online tool to instantly convert any hex code to a UIColor code snippet for Swift and Objective-C, for when it's inconvenient to use custom methods or plugins: http://ebelinski.com/uihex/
我创建了一个在线工具,可以立即将任何十六进行代码转换为Swift和Objective-C的UIColor代码片段,因为当使用自定义方法或插件时不方便:http://ebelinski.com/uihex/。
#13
1
If converting from ObjectiveC to swift is hard for you, here's the answer with Swift. Currently it only takes strings without the #, but you can add a scanner method to skip it I believe.
如果从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)/225,
green: CGFloat((hexInt & 0xFF00) >> 8)/225,
blue: CGFloat((hexInt & 0xFF))/225,
alpha: 1)
return color
}
#14
0
I ended up creating a category for UIColor
that I can just reuse in my other projects. Github: https://github.com/mattquiros/UIColorHexColor
我最终为UIColor创建了一个类别,我可以在其他项目中重用它。Github:https://github.com/mattquiros/UIColorHexColor
The usage goes like:
使用是一样的:
UIColor *customRedColor = [UIColor colorFromHex:0x990000];
This is far faster than passing on a string and converting it to a number then shifting the bits. You can also import the category from inside your .pch
file so you can easily use colorFromHex
everywhere in your app like it's built-in to UIColor
:
这比传递一个字符串并将其转换为数字然后移动比特的速度要快得多。你也可以从你的.pch文件中导入这个类别,这样你就可以在你的应用程序中随处使用colorFromHex,比如它内置到UIColor:
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
// Your other stuff here...
#import "UIColor+HexColor.h"
#endif
#15
0
I think there is an even easier way when using HEX values. Just add a definition at the top of your file or reference a header file for the conversion (UIColorFromRGB). You can even add a template of fixed HEX color values.
我认为使用十六进制值更容易。只需在文件的顶部添加一个定义,或者引用一个用于转换的头文件(UIColorFromRGB)。您甚至可以添加固定十六进制颜色值的模板。
#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]
Then just reference it in your code using the HEX values directly or your defined hex values. For example...
然后在代码中直接引用HEX值或定义的十六进制值。例如……
[myButton1 setTitleColor:UIColorFromRGB(0xd02d2d) forState:UIControlStateNormal];
[myButton2 setTitleColor:UIColorFromRGB(CLR_GREEN_TEXT) forState:UIControlStateNormal];
[myButton3 setTitleColor:UIColorFromRGB(CLR_YELLOW_TEXT) forState:UIControlStateNormal];
(PS - This assumes an Alpha of 1.0, but it can always be changed in the definition).
(PS -这个假设是1.0的Alpha值,但是在定义中它总是可以改变的)。
Enjoy.
享受。
#16
0
I found a cocoapod library very useful in creating UIColor using "#RRGGBB" values.
我发现一个cocoapod库非常有用,它可以使用“#RRGGBB”值创建UIColor。
pod 'UIColor-HexRGB'
#17
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];
}
}