Created
March 27, 2014 06:47
-
-
Save hachinobu/9801700 to your computer and use it in GitHub Desktop.
16進数カラーコードからUIColorを作成する
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
+ (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