Convert hexadecimal color to UIColor in IOS
Easy to use
+ (UIColor *) colorWithHexString: (NSString *) color
{
NSString * cString = [[colorstringByTrimmingCharactersInSet: [NSCharacterSetwhitespaceAndNewlineCharacterSet] uppercaseString];
// String shocould be 6 or 8 characters
If ([cString length] <6 ){
Return [UIColorclearColor];
}
// Strip 0X if it appears
If ([cString hasPrefix: @ "0X"])
CString = [cStringsubstringFromIndex: 2];
If ([cString hasPrefix: @ "#"])
CString = [cStringsubstringFromIndex: 1];
If ([cString length]! = 6)
Return [UIColorclearColor];
// Separate into r, g, B substrings
Nsange range;
Range. location = 0;
Range. length = 2;
// R
NSString * rString = [cString substringWithRange: range];
// G
Range. location = 2;
NSString * gString = [cString substringWithRange: range];
// B
Range. location = 4;
NSString * bString = [cString substringWithRange: range];
// Scan values
Unsigned int r, g, B;
[[NSScannerscannerWithString: rString] scanHexInt: & r];
[[NSScannerscannerWithString: gString] scanHexInt: & g];
[[NSScannerscannerWithString: bString] scanHexInt: & B];
Return [uicolorwithred :( (float) r/255.0f) green :( (float) g/255.0f) blue :( (float) B/255.0f) alpha: 1.0f];
}