How do I define constant values of UIColor?
I want to do something like this, but I cannot get a coopera开发者_开发问答tive syntax.
static const UIColor *colorNavbar = [UIColor colorWithRed: 197.0/255.0 green: 169.0/255.0 blue: 140.0/255.0 alpha: 1.0];
I suppose that I could define macros, but they are ugly.
I like to use categories to extend classes with new methods for this sort of thing. Here's an excerpt of code I just wrote today:
@implementation UIColor (Extensions)
+ (UIColor *)colorWithHueDegrees:(CGFloat)hue saturation:(CGFloat)saturation brightness:(CGFloat)brightness {
return [UIColor colorWithHue:(hue/360) saturation:saturation brightness:brightness alpha:1.0];
}
+ (UIColor *)aquaColor {
return [UIColor colorWithHueDegrees:210 saturation:1.0 brightness:1.0];
}
+ (UIColor *)paleYellowColor {
return [UIColor colorWithHueDegrees:60 saturation:0.2 brightness:1.0];
}
@end
Now in code I can do things like:
self.view.backgroundColor = highlight? [UIColor paleYellowColor] : [UIColor whitecolor];
and my own defined colors fit right in alongside the system-defined ones.
(Incidentally, I am starting to think more in terms of HSB than RGB as I pay more attention to colors.)
UPDATE regarding precomputing the value: My hunch is that it's not worth it. But if you really wanted, you could memoize the values with static variables:
+ (UIColor *)paleYellowColor {
static UIColor *color = nil;
if (!color) color = [UIColor colorWithHueDegrees:60 saturation:0.2 brightness:1.0];
return color;
}
You could make a macro do do the memoizing, too.
I usually make a category of UIColor for each project:
@interface UIColor (ProjectName)
+(UIColor *) colorForSomeTable;
+(UIColor *) colorForSomeControl;
+(UIColor *) colorForSomeText;
@end
With the constants in the implementation:
@implementation UIColor (ProjectName)
+(UIColor *) colorForSomeTable { return [UIColor colorWithRed:...]; }
@end
I also do the same for UIFont and UIImage as needed.
To expand on jasoncrawford's answer (I'd put this in as a comment, but you can't format code in the comments) if you want to precompute the values (or do it only once).
+ (UIColor *)paleYellowColor
{
static UIColor* paleYellow = nil;
if (paleYellow == nil)
{
paleYellow = [UIColor colorWithHueDegrees:60 saturation:0.2 brightness:1.0];
}
return paleYellow;
}
The reason your original idea doesn't work is because the compiler can only use initialisers outside of functions, not normal code. You could have achieved something like what you wanted with the initialize methosd e.g.
static UIColor* colorNavBar = nil;
+(void) initialize
{
if (colorNavBar != nil)
{
colorNavBar = ....
}
}
NB the const
qualifier on your original definition is redundant since UIColor
is immutable anyway.
You can 'define' a similar CONSTANT like this:
#define FAV_COLOR [UIColor colorWithRed:24/255.0f green:89/255.0f blue:36/255.0f alpha:0.9]
and call it by name like you are used to with constants: FAV_COLOR
Hope that helps.
You can do this:
#define backgroundColorApp [UIColor colorWithRed: 197.0/255.0 green: 169.0/255.0 blue: 140.0/255.0 alpha: 1.0]
In Swift, define an extension:
extension UIColor {
class func XXXWhiteColor() -> UIColor {
return UIColor(red: 256, green: 256, blue: 256, alpha: 1.0)
}
class func XXXGreenColor() -> UIColor {
return UIColor(red: 73/255.0, green: 212/255.0, blue: 86/255.0, alpha: 1.0)
}
}
Use like:
label.background = UIColor.XXXWhiteColor()
#define textColorApp [UIColor colorWithRed: 197.0/255.0 green: 169.0/255.0 blue: 140.0/255.0 alpha: 1.0]
myLabel.textColor=textColorApp;
Just define below macro in your constant file and pass only RGB value and use anywhere you want
#define RGBCOLOR(r,g,b)[UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1]
To use :
[lblCount setTextColor:RGBCOLOR(236, 43, 92)];
I feel it's also worth mentioning another awesome feature that is rarely talked about: Color Literals. Not only are they easier to read, but they are WAY easier to edit. In Swift,
let color: UIColor = #colorLiteral(red: 0.9607843137, green: 0.4784313725, blue: 0.3215686275, alpha:
When pasted into Xcode, this syntax creates a simple color box. Click here to see an example.
Once you see the box, you can then double click on it to edit it easily. Similarly, you can switch between various IB options, including RGB Sliders, if you have a specific list of color hex values from your designer.
精彩评论