There are many places in my app that needs to set the UIColor. I want to define the color somewhere that I can reuse it without writing the same line again, it's hard to keep track and maintain.
UIColor *myColor = [UIColor colorWithRed:0.1 green:0.3 blue:0.7 alpha:1];
I tried to make it like
#define myColor = [UIColor colorWithRed:0.1 green:0.3 blue:0.7 alpha:1];
and reuse myColor but it doesn't work. :/
Thanks!
For your define, you could write:
#define myColor [UIColor colorWithRed:0.1 green:0.3 blue:0.7 alpha:1]
and where you use myColor, it'll be replaced outright with [UIColor colorWithRed:0.1 green:0.3 blue:0.7 alpha:1].
Alternatively, you could write a category on UIColor that provides a method that returns your colour.
Example:
@interface UIColor (MyColors)
+ (UIColor *)myAwesomeColor;
@end
@implementation UIColor (MyColors)
+ (UIColor *)myAwesomeColor
{
return [UIColor colorWithRed:0.1 green:0.3 blue:0.7 alpha:1];
}
@end
You would then use this like [UIColor myAwesomeColor] wherever you need it, just like you do with [UIColor blackColor].
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With