如何将UIColor转换为HEX并在NSLog中显示 [英] How to convert UIColor to HEX and display in NSLog
问题描述
我已经检查了几个如何将UIColor代码转换为HEX的链接但是我不确定如何调用该方法在NSLog中显示它们。我没有得到评论的声誉,因此发布问题是我的最后手段。 我希望在日志中运行我的应用时显示。
I have checked several links on how to convert UIColor codes to HEX however I am not sure on how to call to the method to display them in NSLog. I haven't got the reputation to comment so posting as a question is my last resort. I want it to display when I run my app in the log.
其次,我在哪里输入RGB颜色编号(R = 30 ,G = 171,B = 13)?我看到所有的例子都使用Array [0],[1],[2],它们通常是指索引位置,那么我在哪里添加颜色值?
Second, where do I input the RGB color number ( R = 30, G = 171, B = 13)? I see that all examples use Array [0], [1], [2] which normally refers to index position, so where do I add the color values?
I有这样的代码:
- (NSString *) hexFromUIColor:(UIColor *)color {
if (CGColorGetNumberOfComponents(color.CGColor) < 4) {
const CGFloat *components = CGColorGetComponents(color.CGColor);
color = [UIColor colorWithRed:components[30] green:components[141] blue:components[13] alpha:components[1]];
}
if (CGColorSpaceGetModel(CGColorGetColorSpace(color.CGColor)) != kCGColorSpaceModelRGB) {
return [NSString stringWithFormat:@"#FFFFFF"];
}
return [NSString stringWithFormat:@"#%02X%02X%02X", (int)((CGColorGetComponents(color.CGColor))[0]*255.0), (int)((CGColorGetComponents(color.CGColor))[1]*255.0), (int)((CGColorGetComponents(color.CGColor))[2]*255.0)];
}
我检查的链接:
我试图在viewDidLoad中调用该方法,但是如果没有UIColor它将无法工作。我确信这很简单。
I have tried to call the method in viewDidLoad however it wont work without UIColor. I am sure it's something simple.
感谢有人回答。
我在viewDidLoad中使用的代码是什么来调用此方法为了在NSLog中显示?
What is the code I use in my viewDidLoad to call to this method in order to display in NSLog?
推荐答案
使用以下代码:
- (NSString *)hexStringFromColor:(UIColor *)color {
const CGFloat *components = CGColorGetComponents(color.CGColor);
CGFloat r = components[0];
CGFloat g = components[1];
CGFloat b = components[2];
return [NSString stringWithFormat:@"#%02lX%02lX%02lX",
lroundf(r * 255),
lroundf(g * 255),
lroundf(b * 255)];
}
获取十六进制代码字符串后,调用以下方法获取 UIColor
After getting hex code string, Call below method to get UIColor
- (UIColor *) colorWithHexString: (NSString *) hexString
{
NSString *colorString = [[hexString stringByReplacingOccurrencesOfString: @"#" withString: @""] uppercaseString];
NSLog(@"colorString :%@",colorString);
CGFloat alpha, red, blue, green;
// #RGB
alpha = 1.0f;
red = [self colorComponentFrom: colorString start: 0 length: 2];
green = [self colorComponentFrom: colorString start: 2 length: 2];
blue = [self colorComponentFrom: colorString start: 4 length: 2];
return [UIColor colorWithRed: red green: green blue: blue alpha: alpha];
}
`
- (CGFloat) colorComponentFrom: (NSString *) string start: (NSUInteger) start length: (NSUInteger) length {
NSString *substring = [string substringWithRange: NSMakeRange(start, length)];
NSString *fullHex = length == 2 ? substring : [NSString stringWithFormat: @"%@%@", substring, substring];
unsigned hexComponent;
[[NSScanner scannerWithString: fullHex] scanHexInt: &hexComponent];
return hexComponent / 255.0;
}
USAGE
USAGE
// ( R = 30, G = 171, B = 13)?
CGFloat red = 30.0;
CGFloat green = 171.0;
CGFloat blue = 13.0;
CGFloat alpha = 255.0
UIColor *color = [UIColor colorWithRed:(red/255.0) green:(green/255.0) blue:(blue/255.0) alpha:(alpha/255.0)];
NSString *colorCode = [self hexStringFromColor:color];
NSLog(@"Color Code: %@", colorCode);
UIColor *resultColor = [self colorWithHexString:colorCode];
这篇关于如何将UIColor转换为HEX并在NSLog中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!