生成随机UIColor

时间:2014-01-15 06:24:17

标签: objective-c uicolor

我尝试为UILabel获取随机颜色...

- (UIColor *)randomColor
{
    int red = arc4random() % 255 / 255.0;
    int green = arc4random() % 255 / 255.0;
    int blue = arc4random() % 255 / 255.0;
    UIColor *color = [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
    NSLog(@"%@", color);
    return color;
}

并使用它:

[mat addAttributes:@{NSForegroundColorAttributeName : [self randomColor]} range:range];

但颜色总是黑色的。怎么了?

P.S。对不起我的英文)

10 个答案:

答案 0 :(得分:34)

[UIColor colorWithHue:drand48() saturation:1.0 brightness:1.0 alpha:1.0];

或在Swift中:

UIColor(hue: CGFloat(drand48()), saturation: 1, brightness: 1, alpha: 1)

随意随意或根据自己的喜好调整饱和度和亮度。

答案 1 :(得分:23)

因为您已将颜色值分配给int个变量。使用float (或CGFloat)代替。另外(作为@stackunderflow's said),其余部分必须 取模256为了覆盖整个范围0.0 ... 1.0

CGFloat red = arc4random() % 256 / 255.0;
// Or (recommended):
CGFloat red = arc4random_uniform(256) / 255.0;

答案 2 :(得分:6)

试试这个

CGFloat hue = ( arc4random() % 256 / 256.0 );  //  0.0 to 1.0
CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 to 1.0, away from white
CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 to 1.0, away from black
UIColor *color = [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];

答案 3 :(得分:5)

你可以用这种方式,

NSInteger aRedValue = arc4random()%255;
NSInteger aGreenValue = arc4random()%255;
NSInteger aBlueValue = arc4random()%255;

UIColor *randColor = [UIColor colorWithRed:aRedValue/255.0f green:aGreenValue/255.0f blue:aBlueValue/255.0f alpha:1.0f];


答案 4 :(得分:5)

这是一个快速版本,制作成UIColor扩展名:

extension UIColor {

    class func randomColor(randomAlpha randomApha:Bool = false)->UIColor{

        let redValue = CGFloat(arc4random_uniform(255)) / 255.0;
        let greenValue = CGFloat(arc4random_uniform(255)) / 255.0;
        let blueValue = CGFloat(arc4random_uniform(255)) / 255.0;
        let alphaValue = randomApha ? CGFloat(arc4random_uniform(255)) / 255.0 : 1;

        return UIColor(red: redValue, green: greenValue, blue: blueValue, alpha: alphaValue)

    }
}

答案 5 :(得分:2)

// RGB
UIColor *randomRGBColor = [[UIColor alloc] initWithRed:arc4random()%256/256.0 
                                                 green:arc4random()%256/256.0 
                                                  blue:arc4random()%256/256.0 
                                                 alpha:1.0];

// HSB
UIColor *randomHSBColor = [[UIColor alloc] initWithHue:arc4random()%256/256.0 
                                            saturation:(arc4random()%128/256.0)+0.5 
                                            brightness:(arc4random()%128/256.0)+0.5 
                                                 alpha:1.0];

答案 6 :(得分:1)

arc4random() % 255 / 255.0将始终被截断为0,因为arc4random()%255将是0到254之间的整数,并且除以255.0并且转换为int将始终导致0.您应该保存结果作为一个浮动代替。

(如果您想从所有可能的颜色中随机选择,也应使用arc4random()%256。)

答案 7 :(得分:1)

这是一个片段:

CGFloat redLevel    = rand() / (float) RAND_MAX;
CGFloat greenLevel  = rand() / (float) RAND_MAX;
CGFloat blueLevel   = rand() / (float) RAND_MAX;

self.view.backgroundColor = [UIColor colorWithRed: redLevel
                                            green: greenLevel
                                             blue: blueLevel
                                            alpha: 1.0];

答案 8 :(得分:0)

删除3行arc4random的重复:)

static func randomColor() -> UIColor {
    let random = {CGFloat(arc4random_uniform(255)) / 255.0}
    return UIColor(red: random(), green: random(), blue: random(), alpha: 1)
}

答案 9 :(得分:0)

使用类var random

Swift 解决方案:

extension UIColor {
    class var random: UIColor {
        return UIColor(red: .random(in: 0...1), green: .random(in: 0...1), blue: .random(in: 0...1), alpha: 1.0)
    }
}

与其他任何内置UIColor类变量(.red.blue.white等)一样使用,例如:

view.backgroundColor = .random