Reputation: 4615
I'm new to iPhone development and are looking for at way to use color resources (like in Android). I want to apply them on text and panel backgrounds etc.
The ultimate goal is to be able to switch color resources while keeping the code and xib files intact. In this way it shall be possible to for example make several versions of the app with different text colors by just changing the rgb value in one single location.
I know it is possible to switch the image resources in this way, but havent seen anything similar with the colors.
Appreciating any help
Upvotes: 0
Views: 438
Reputation: 9126
The UIColor
class is quite amazing.
It has a lot of nice, desirable colors available out of the box. For instance:
[UIColor clearColor];
[UIColor redColor];
[UIColor greenColor];
[UIColor blueColor];
[UIColor lightTextColor];
[UIColor groupTableViewBackgroundColor];
On top of it, you can generate all sorts of nifty things with a little math and some macros. This one lets you generate colors from 8-bit hex values (i.e 0x000000
for black).
#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]
You can also load a UIColor
object from an image.
[UIColor colorWithPatternImage:[UIImage imageNamed:@"myImage.png]];
To use any UIColor object, you can simply do this:
UITextField.textColor = [UIColor magentaColor];
Just note that the property you're assigning has to be a UIColor property (obviously).
Edit: For what you want to do, this may be the best solution:
VJColorConstants
.+(UIColor *)prettyColor{
return UIColorFromRGB(0x4A6B82);
}
There are about 100 spins on how to return a constant, but I like that one. After you have that file set up, you can do this:
UIView.backgroundColor = [VJColorConstants prettyColor];
Upvotes: 1
Reputation: 90117
The only way I could think of is by using #define preprocessor macros. But then you have to set all your colors in code. You have to connect all your views to IBOutlets and so on.
#if VERSION_PINK
#define kBackGroundColor [UIColor colorWithRed:255/255.0f green:105/255.0f blue:180/255.0f alpha:1]
#elif VERSION_GREEN
#define kBackGroundColor [UIColor colorWithRed:155/255.0f green:255/255.0f blue:155/255.0f alpha:1]
#else
#define kBackGroundColor [UIColor colorWithRed:150/255.0f green:150/255.0f blue:150/255.0f alpha:1]
#endif
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backGroundColor = kBackGroundColor;
}
You would then set VERSION_PINK=1, VERSION_GREEN=1 and so on in the Preprocessor Macros in the build settings of your app.
Upvotes: 0
Reputation: 1564
umm not sure I understand your question properly, but my suggestion would be to take a Global constant for the color. That way you can change it from one particular loacation.
#define CUSTOM_COLOR [UIColor colorWithRed:0.0 green:0.5607 blue:0.8627 alpha:0.50]
Upvotes: 0