How to determine the constant values ​​of UIColor?

I want to do something like this, but I cannot get the shared 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 I can define macros, but they are ugly.

+57
objective-c cocoa-touch const uicolor
Apr 27 '10 at 3:26
source share
9 answers

I like to use categories to extend classes with new methods for this kind of thing. Here is a snippet 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 the code, I can do things like:

 self.view.backgroundColor = highlight? [UIColor paleYellowColor] : [UIColor whitecolor]; 

and my own specific colors go straight to system ones.

(By the way, I'm starting to think more about HSB than about RGB, as I pay more attention to colors.)

UPDATE regarding precomputing a value: My hunch is that it is not worth it. But if you really wanted to, 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 can make a macro, also do memoizing.

+123
Apr 27 2018-10-12T00:
source share

I usually make the UIColor category for each project:

 @interface UIColor (ProjectName) +(UIColor *) colorForSomeTable; +(UIColor *) colorForSomeControl; +(UIColor *) colorForSomeText; @end 

With constants in implementation:

 @implementation UIColor (ProjectName) +(UIColor *) colorForSomeTable { return [UIColor colorWithRed:...]; } @end 

I also do the same for UIFont and UIImage as needed.

+35
Apr 27 '10 at 4:42 on
source share

You can "define" a similar CONSTANT as follows:

 #define FAV_COLOR [UIColor colorWithRed:24/255.0f green:89/255.0f blue:36/255.0f alpha:0.9] 

and name it by name, as you are used to constants: FAV_COLOR

Hope this helps.

+11
May 28 '13 at 11:29
source share

To extend the answer to jasoncrawford (I would put this as a comment, but you cannot format the 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 is not working is because the compiler can only use initializers outside functions, not regular code. You could achieve something like what you wanted with methosd initialization, for example.

 static UIColor* colorNavBar = nil; +(void) initialize { if (colorNavBar != nil) { colorNavBar = .... } } 

The NB specifier const in the original definition is redundant, since UIColor is unchanged in any case.

+10
Apr 27 '10 at 8:21
source share

You can do it:

 #define backgroundColorApp [UIColor colorWithRed: 197.0/255.0 green: 169.0/255.0 blue: 140.0/255.0 alpha: 1.0] 
+7
Dec 30 '11 at 8:37
source share

In Swift: Define 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 as: Label.background = UIColor.XXXWhiteColor ()

+5
Apr 19 '15 at 18:41
source share
 #define textColorApp [UIColor colorWithRed: 197.0/255.0 green: 169.0/255.0 blue: 140.0/255.0 alpha: 1.0] myLabel.textColor=textColorApp; 
+4
Apr 11 '14 at 6:51
source share

Just define the macro below in your permanent file and pass only the RGB value and use anywhere

 #define RGBCOLOR(r,g,b)[UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1] 

Use:

 [lblCount setTextColor:RGBCOLOR(236, 43, 92)]; 
+1
Sep 23 '15 at 11:19
source share

I think it's also worth mentioning another amazing feature rarely talked about: Color Literals. Not only are they easier to read, but they are easy 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 field. Click here to see an example.

Once you see the window, you can double-click on it to easily edit it. Similarly, you can switch between different IB options, including RGB Sliders , if you have a specific list of hexadecimal color values ​​from your designer.

0
Dec 14 '16 at 21:41
source share



All Articles