How do I convert NSString to rgbValue and pass that as value as parameter for UIColorFromRGB?
I'm working on an application where a color code such as (#ff0000) is read from an xml file in the documents dir. I have defined a Macro UIColorFromRGB which is also working fine when I pass the color code directly as below:
bgView开发者_开发技巧.backgroundColor = UIColorFromRGB(0xff0000);
What should I Change if I have to pass the code as below:
bgView.backgroundColor = UIColorFromRGB(mycolor);
where (mycolor) is NSString obj that contains value (#ff0000) obtained from the xml file?
Greatly appreciate your help!!!
NSScanner
has a -scanHexInt:
method that you can use to convert the string into an integer.
NSScanner *scanner = [NSScanner scannerWithString:mycolor];
if ([scanner scanString:@"#" intoString:NULL]) {
unsigned int colorValue = 0;
if ([scanner scanHexInt:&colorValue]) {
...
}
}
精彩评论