In this post I’ll demonstrate how to create a category of the UIColor class to add a method for creating a UIColor object from a hex value. That’s a mouthful – if you need to create a UIColor object from a hex string, read on…
Assume you have the value @”0A5CEA” (an NSString object that holds a hex value) and need a UIColor object that represents the value of the string. With the code below, creating the UIColor object is as easy as:
UIColor *color = [UIColor colorwithHexString:@"0A5CEA" alpha:.9];
Notice that I’ve also provided a parameter to set the alpha value.