编辑:(对于 ios)
-(NSDictionary *)randomColorAndLabel{
UIColor *color1= [UIColor colorWithRed:0.2 green:0.4 blue:0.3 alpha:1.0];
UIColor *color2= [UIColor colorWithRed:0.3 green:0.4 blue:0.3 alpha:1.0];
UIColor *color3= [UIColor colorWithRed:0.4 green:0.4 blue:0.3 alpha:1.0];
NSDictionary *colorDict1=@{@"color1" : color1};
NSDictionary *colorDict2=@{@"color2" : color2};
NSDictionary *colorDict3=@{@"color3" : color3};
NSArray *colors=@[colorDict1, colorDict2, colorDict3];
NSInteger randomNumber=arc4random()%3;
return colors[randomNumber];
}
它返回带有颜色和名称的字典,可用于标签。
较早解决了 OSX
我解决了 OSX,使用 NSColor 而不是 UIColor 并且方法名称更改为
colorWithRed:randomRed green:randomGreen blue:randomBlue alpha:1.0];
-(NSDictionary *)randomColorAndLabel{
NSColor *color1= [NSColor colorWithCalibratedRed:0.2 green:0.4 blue:0.3 alpha:1.0];
NSColor *color2 = [NSColor colorWithCalibratedRed:0.3 green:0.4 blue:0.3 alpha:1.0];
NSColor *color3= [NSColor colorWithCalibratedRed:0.4 green:0.4 blue:0.3 alpha:1.0];
NSDictionary *colorDict1=@{@"color1" : color1};
NSDictionary *colorDict2=@{@"color2" : color2};
NSDictionary *colorDict3=@{@"color3" : color3};
NSArray *colors=@[colorDict1, colorDict2, colorDict3];
NSInteger randomNumber=arc4random()%3;
return colors[randomNumber];
}
这将返回带有名称和颜色的字典。