Wednesday, February 23, 2011

NSStringToUIColor with alpha

+(UIColor*)toUIColor:(NSString*)hexColorString {
unsigned int c;
if ([hexColorString characterAtIndex:0] == '#') {
[[NSScanner scannerWithString:[hexColorString substringFromIndex:1]] scanHexInt:&c];
} else {
[[NSScanner scannerWithString:hexColorString] scanHexInt:&c];
}
if([hexColorString length] > 7) {
return [UIColor colorWithRed:((c & 0xff000000) >> 24)/255.0 green:((c & 0xff0000) >> 16)/255.0 blue:((c & 0xff00) >> 8)/255.0 alpha:(c & 0xff)/255.0];
}
return [UIColor colorWithRed:((c & 0xff0000) >> 16)/255.0 green:((c & 0xff00) >> 8)/255.0 blue:(c & 0xff)/255.0 alpha:1.0];
}

[Color toUIColor:#001122];
[Color toUIColor:#001122aa];

No comments:

Post a Comment