生成随机的 UIColor

问题描述 投票:0回答:10

我尝试为 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];

但颜色始终是黑色。怎么了?

objective-c uicolor
10个回答
48
投票
[UIColor colorWithHue:drand48() saturation:1.0 brightness:1.0 alpha:1.0];

或在 Swift 中:

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

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


27
投票

因为您已将颜色值分配给

int
变量。使用
float
(或
CGFloat
)。另外(正如@stackunderflow所说的),其余部分必须 以 256 为模以覆盖整个范围
0.0 ... 1.0
:

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

14
投票

这是一个 swift 版本,做成了

UIColor
扩展:

extension UIColor {
    class func randomColor(randomAlpha: 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 = randomAlpha ? CGFloat(arc4random_uniform(255)) / 255.0 : 1;

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

13
投票
使用类 var 的

Swift 解决方案

random
:

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

9
投票

试试这个

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];

6
投票

以下适用于 iOS 12

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];



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];

1
投票

arc4random() % 255 / 255.0
将始终被截断为 0,因为
arc4random()%255
将是 0 到 254(含)之间的整数,除以 255.0 并转换为 int 将始终得到 0。您应该将结果保存为浮点数。

(如果您想从所有可能的颜色中随机选择,也应该使用

arc4random()%256
。)


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];

1
投票

删除重复的 3

arc4random
行:)

static func randomColor() -> UIColor {
    let random = { CGFloat(arc4random_uniform(255)) / 255.0 }
    return UIColor(red: random(), green: random(), blue: random(), alpha: 1)
}
© www.soinside.com 2019 - 2024. All rights reserved.