Created
May 31, 2017 01:15
-
-
Save 5ZSQ/cbc6d561753859921a00dbca8d571452 to your computer and use it in GitHub Desktop.
iOS - 颜色值#ffffff转UIColor
This file contains 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*)stringToConvert{ | |
if([stringToConverthasPrefix:@"#"]) | |
{ | |
stringToConvert = [stringToConvertsubstringFromIndex:1]; | |
} | |
NSScanner*scanner = [NSScannerscannerWithString:stringToConvert]; | |
unsignedhexNum; | |
if(![scannerscanHexInt:&hexNum]) | |
{ | |
returnnil; | |
} | |
return[UIColorcolorWithRGBHex:hexNum]; | |
} | |
+ (UIColor*)colorWithRGBHex:(UInt32)hex{ | |
intr = (hex >>16) &0xFF; | |
intg = (hex >>8) &0xFF; | |
intb = (hex) &0xFF; | |
return[UIColorcolorWithRed:r /255.0f | |
green:g /255.0f | |
blue:b /255.0f | |
alpha:1.0f]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment