Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/99.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何获得合适的颜色格式_Ios_Objective C - Fatal编程技术网

Ios 如何获得合适的颜色格式

Ios 如何获得合适的颜色格式,ios,objective-c,Ios,Objective C,在我的编辑器中,我有一个标签,在UITapGestureRecognitizer上,我需要获取文本的文本颜色 NSLog(@"Fontcolor---- %@",sanlabel.textColor); 但是这个代码返回给我 UIDeviceRGBColorSpace 0.686275 1 1 1 我如何获得正确格式的颜色代码,以便我可以将其作为 UIColorFromRGB(0X2c3836) 您可以使用此方法(取自): -(NSString*)htmlFromUIColor:(UICo

在我的编辑器中,我有一个标签,在UITapGestureRecognitizer上,我需要获取文本的文本颜色

NSLog(@"Fontcolor---- %@",sanlabel.textColor);
但是这个代码返回给我

UIDeviceRGBColorSpace 0.686275 1 1 1
我如何获得正确格式的颜色代码,以便我可以将其作为

UIColorFromRGB(0X2c3836)
您可以使用此方法(取自):

-(NSString*)htmlFromUIColor:(UIColor*)\U颜色{
如果(CGColorGetNumberOfComponents(_color.CGColor)<4){
const CGFloat*components=CGColorGetComponents(_color.CGColor);
_颜色=[UIColor colorWithRed:components[0]绿色:components[0]蓝色:components[0]alpha:components[1];
}
如果(CGColorSpaceGetModel(CGColorGetColorSpace(_color.CGColor))!=kCGColorSpaceModelRGB){
返回[NSString stringWithFormat:@“#FFFFFF”];
}    
返回[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)];
}

我假设您正在设置标签的文本,这是某种颜色定义,然后您想知道点击了哪个标签

我认为这是错误的方法,更好的方法是使用标签的标签索引到颜色定义数组中。这不仅更快,而且允许您稍后更改标签的文本格式(可能是为了国际化),而不会影响功能

假设有3个标签;定义每个标签的颜色:

static unsigned _labelColours[3] = {
    0x2c3836, 0x2c3837, 0x2c3838
};
现在将3个标签的标签设置为从100开始(例如)。这可以在代码中完成,也可以在IB中完成

然后,当您想知道指定给标签的颜色是什么时,只需在操作方法中执行以下操作:

- (IBAction)labelWasTouched:(id)sender {
    NSInteger tag = [sender tag];
    NSAssert(tag >= 100 && tag <= 102, @"Tag out-of-range");
    unsigned colour = _labelColours[tag - 100];
    UIColor *colourObj = UIColorFromRGB(colour);
}
-(iAction)标签被触摸:(id)发送方{
NSInteger标签=[sender标签];

NSAssert(tag>=100&&tag轻松将UIDeviceRBGColorSpace中的每个值乘以255。然后使用NSString::stringWithFormat将值转换为十六进制,并使用%x

此外:


NSLog(@“Fontcolor----%@”,sanlabel.textColor);返回颜色格式,然后作为一个个体如何获得0.686275。当颜色由[UIColor WithWhite:]创建时,您得到了2个值。我们给您4个值。然后拆分字符串并将4个(或2个)部分转换为int。但这有点脏……我已经尝试过了[getcolor componentsSeparatedByString:@”“]但它给了我一个例外哇。这里有一些代码:NSString*test=[NSString stringWithFormat:@“%@”,lblTest.textColor];NSArray*components=[test componentsSeparatedByString:@”“];int r,g,b;r=[[components objectAtIndex:0]intValue];g=[[components objectAtIndex:1]intValue];b=[[components ObjectatinIndex:2]intValue];r*=255;g*=255;b*=255;NSString*HextStr=[NSString stringWithFormat:@“%x%x%x”,r,g,b];NSLog(@“test:%@@@i%i%i”,HextStr,r,g,b);Mirko Brunner我算出了这是ans components=CGColorGetComponents(sanlabel.textColor.CGColor);r=(int)(组件[0]g=(int)(分量[1]*255);b=(int)(分量[2]*255);
- (IBAction)labelWasTouched:(id)sender {
    NSInteger tag = [sender tag];
    NSAssert(tag >= 100 && tag <= 102, @"Tag out-of-range");
    unsigned colour = _labelColours[tag - 100];
    UIColor *colourObj = UIColorFromRGB(colour);
}
int r, g, b, a;

r = (int)(0.686275 * 255);
g = b = 1*255;

NSString *hexStr = [NSString stringWithFormat:@"%x%x%x",r,g,b];