Pick a random color in iOS

In one of my methods I wanted to put randomly colored text on the screen. I put this class method in my Utilities.m class.


#define ARC4RANDOM_MAX      0x100000000
+ (UIColor *) randomColor {
    CGFloat red =  (CGFloat)arc4random()/ARC4RANDOM_MAX;
    CGFloat blue = (CGFloat)arc4random()/ARC4RANDOM_MAX;
    CGFloat green = (CGFloat)arc4random()/ARC4RANDOM_MAX;
    return [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
}

To set the color, just call


label.textColor = [Utilities randomColor];

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.