Skip to content

Instantly share code, notes, and snippets.

@hachinobu
Created March 27, 2014 06:47
Show Gist options
  • Save hachinobu/9801700 to your computer and use it in GitHub Desktop.
Save hachinobu/9801700 to your computer and use it in GitHub Desktop.
16進数カラーコードからUIColorを作成する
+ (UIColor *)colorWithHexString:(NSString *)hex
{
//先頭に#がついていた場合は#を削除
if ([hex hasPrefix:@"#"]) {
hex = [hex substringFromIndex:1];
}
unsigned int rgb[3];
for (int i = 0; i < 3; i++) {
NSString *component = [hex substringWithRange:NSMakeRange(i * 2, 2)];
NSScanner *scanner = [NSScanner scannerWithString:component];
[scanner scanHexInt:&rgb[i]];
}
return [UIColor colorWithRed:rgb[0]/255.0 green:rgb[1]/255.0 blue:rgb[2]/255.0 alpha:1.0f];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment