I am working on trying to make an app that lets the colors of things like navigation bars to be changed server-side. In my app delegate I set up a definition for using Hex codes for UIColors like this:
#define UIColorFromRGB(rgbValue) [UIColor \
colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
green:((float)((rgbValue & 0xFF00) >> 8))/255.0 \
blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
My thought was to create a class on a Parse server that includes columns for different elements of the app, with each entry containing a string that looked like "0xFF0000". Then I could query Parse, retrieve it, convert the string to an int value, and plug it in to
UIColor *tabBarColor = UIColorFromRGB(valueFromParse);
However, it seems the x in the middle of everything causes me issues. Any suggestions?
You can convert the hex string to a numeric value with NSScanner:
For example:
NSString *s = @"0x00ff00";
unsigned result = 0;
NSScanner *scanner = [NSScanner scannerWithString:s];
[scanner scanHexInt:&result];
UIColor *c = UIColorFromRGB(result);
self.view.backgroundColor = c;
You would, of course, want to implement error handling / string format checking...