从uicolor的六角形颜色 [英] hex color from uicolor

查看:135
本文介绍了从uicolor的六角形颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将uicolor转换为十六进制颜色时遇到了问题,这里是我发现的

  CGColorRef colorref = [[Colorview_ backgroundColor] CGColor]; 

int numComponents = CGColorGetNumberOfComponents(colorref);

if(numComponents == 4){
const CGFloat * components = CGColorGetComponents(colorref);

int hexValue = 0xFF0000 * components [0] + 0xFF00 * components [1] + 0xFF * components [2];

NSString * hexString = [NSString stringWithFormat:@#%d,hexValue];
}

这段代码给了我#5576149(例如)hexString,看看有7个数字不是6,它不是一个十六进制颜色,任何帮助将不胜感激,thx。

NSString * hexString = [NSString stringWithFormat:@#%d,hexValue];

您将其格式化为包含%d的数字

你想用十六进制格式化为%x或%X - 也许作为字符串%s我没有检查函数正在做什么以及 int hexValue 是持有


d或i有符号十进制整数392

x无符号十六进制整数7fa

X无符号十六进制整数(大写字母)7FA


i have a problem in converting uicolor to hex color, here what i found

CGColorRef colorref = [[Colorview_ backgroundColor] CGColor];

int numComponents = CGColorGetNumberOfComponents(colorref);

if (numComponents == 4) {
    const CGFloat *components = CGColorGetComponents(colorref);

    int hexValue = 0xFF0000*components[0] + 0xFF00*components[1] + 0xFF*components[2];

    NSString *hexString = [NSString stringWithFormat:@"#%d", hexValue];
}

this code is giving me #5576149 (for example) for hexString, us you see there are 7 digits not 6, it's not a hex color, any help will be appreciated, thx.

解决方案

NSString *hexString = [NSString stringWithFormat:@"#%d", hexValue];

You are formatting it as a digit with %d

You want to format it as hex with %x or %X -- maybe as string %s I didnt check what the function is doing and what int hexValue is holding

d or i Signed decimal integer 392

x Unsigned hexadecimal integer 7fa

X Unsigned hexadecimal integer (capital letters) 7FA

这篇关于从uicolor的六角形颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆