随机化标签的颜色

时间:2013-11-24 21:33:06

标签: random uilabel uicolor

所以我有一个标签,我希望能够按下我设置的按钮并更改标签的RGB值。看似简单,但我很难过。有什么想法吗?

NSInteger r = arc4random()%255;
NSInteger g = arc4random()%255;
NSInteger b = arc4random()%255;

_label.textColor= [UIColor colorWithRed:(arc4random_uniform(r/255.0)) green:(arc4random_uniform(g/255.0)) blue:(arc4random_uniform(b/255.0)) alpha:1] ;

2 个答案:

答案 0 :(得分:1)

你的问题是随机的不必要的双重使用。试试这个:

NSInteger r = arc4random_uniform(255);
NSInteger g = arc4random_uniform(255);
NSInteger b = arc4random_uniform(255);

UIColor *color = [UIColor colorWithRed:r / 255.0 green:g / 255.0 blue:b / 255.0 alpha:1];
_label.textColor = color;

你所拥有的是在0.0和1.0之间的随机值上调用arc4random_uniform

答案 1 :(得分:1)

您需要替换

[UIColor colorWithRed:(arc4random_uniform(r/255.0)) green:(arc4random_uniform(g/255.0)) blue:(arc4random_uniform(b/255.0)) alpha:1] ;

[UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1] ;

正如您当前在已经介于1和0之间的随机值上调用arc4_random_uniform() - 这正是您创建颜色所需的。