Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate a random UIColor

I try to get a random color for 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; } 

And use it:

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

But color is always black. What is wrong?

like image 816
Tatiana Mudryak Avatar asked Jan 15 '14 06:01

Tatiana Mudryak


People also ask

How do I generate a random color in CSS?

There is no way to do this using CSS only. CSS doesn't support logical statements because CSS is purely deterministic (there is nothing like array etc in CSS). We can use client-side JavaScript to select a random color from the array.


1 Answers

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

or in Swift:

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

Feel free to randomise or adjust saturation and brightness to your liking.

like image 141
kkodev Avatar answered Sep 27 '22 23:09

kkodev