Objective C defining UIColor constants

41,964

Solution 1

A UIColor is not mutable. I usually do this with colors, fonts and images. You could easily modify it to use singletons or have a static initializer.

@interface UIColor (MyProject)

+(UIColor *) colorForSomePurpose;

@end

@implementation UIColor (MyProject)

+(UIColor *) colorForSomePurpose { return [UIColor colorWithRed:0.6 green:0.8 blue:1.0 alpha:1.0]; }

@end

Solution 2

For simplicity I did this:

/* Constants.h */
#define myColor [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0]

Don't forget to leave out the ';' so you can use it as a normal expression.

I'm not sure if there's anything technically wrong with this approach, but it works fine, and avoids the compile-time constant initializer error - this code is effectively stuck in place anywhere you put 'myColor', so it doesn't ever get compiled until you actually use it.

Solution 3

Another option

in your .h you can do

extern UIColor *  const COLOR_LIGHT_BLUE;

in your .mm you can do

UIColor* const COLOR_LIGHT_BLUE = [[UIColor alloc] initWithRed:21.0f/255 green:180.0f/255  blue:1 alpha:1];//;#15B4FF

Solution 4

If you're looking for a quick and dirty one without extensions go with clang:

#define kGreenColor colorWithRed:(0/255.0) green:(213/255.0) blue:(90/255.0) alpha:1.0

- (void)doSomething
{
   _label.textColor = [UIColor kGreenColor];

}
Share:
41,964
futureelite7
Author by

futureelite7

I develop in a variety of different languages, mainly in the following areas: Objective-C and iOS development, HTML and CSS, Javascript and jQuery, Java (JSP), C#

Updated on July 01, 2020

Comments

  • futureelite7
    futureelite7 almost 4 years

    I have a iPhone application with a few custom-defined colors for my theme. Since these colors will be fixed for my UI, I would like to define the colors in a class to be included (Constants.h and Constants.m). How do I do that? (Simply defining them does not work because UIColors are mutable, and would cause errors - Initalizer not constant).

    /* Constants.h */
    extern UIColor *test;
    
    /* Constants.m */
    UIColor *test = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0];
    

    Thanks!

  • Jared Pochtar
    Jared Pochtar about 14 years
    -1 for the first sentence - the Initializer is what is not constant, not the instance. For all intents and purposes, UIColor could (or could tomorrow) contain internal state variables which are unexposed to the user, making them seem to us constant, however there is nothing in Obj-C forcing this. Therefore the compiler cannot use a static initializer, like it would like to, because [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0] is a method call, and thus the results of this cannot be put in the text segment, which is created at compile time.
  • drawnonward
    drawnonward about 14 years
    Mutable means that you can change the instance. Immutable means you cannot change the instance. Constant means that the instance cannot be changed. A UIColor is not mutable and also not constant. Only CFString/NSString has truly constant instances because they are handled specially as part of the language by the compiler.
  • mostafa tourad
    mostafa tourad about 12 years
    AppController - I guess you are referring to the UIApplication delegate?
  • Sulthan
    Sulthan about 12 years
    Changing colors at runtime would be much bigger task in most applications. Also good way to pollute the app delegate with code that doesn't have anything to do with it.
  • Greg
    Greg about 12 years
    What is this shifty C++ dynamic constants? ;) +1
  • Taketo Sano
    Taketo Sano almost 11 years
    I'm interested in why this possible too.
  • Daniel S.
    Daniel S. almost 11 years
    It's possible, because T* const is a constant pointer to a regular object (you cannot modify where such a pointer points to), as opposed to const T*, which is a pointer to a constant object (you cannot modify the object through such a pointer).
  • brainray
    brainray over 10 years
    interesting approach! I'd be interested why the ';' can be left out? How is this called?
  • Fateh Khalsa
    Fateh Khalsa over 10 years
    It's a preprocessor macro. Effectively 'myColor' is replaced wherever it appears in your code with the full '[UIColor colorWith... ' line of code, before it ever compiles. You leave out the ';' in the macro so you can use the code like this: UIColor *color = myColor; rather than UIColor *color = myColor, which doesn't look like a normal line of code (it's more natural to place a semicolon at the end of any line of code). It can be left out because preprocessor macros are evaluated as they are when they're used, so as long as you place a semicolon in your actual code, you're fine.
  • Majster
    Majster over 10 years
    But doesn't this create a new instance of this color every time you use this macro? Consider having it in a for loop with 1000 iterations and assigning it as a background color to a view. This is not very efficient is it?
  • Fateh Khalsa
    Fateh Khalsa about 10 years
    That is true, but where are you ever going to use this 1000 times? Also, if you were to create the colors normally, it would be the same case. This just allows you the convenience of defining the color once so it can be changed easily. The other methods mentioned in answers here will of course avoid creating those multiple instances, but they are more work to implement and less elegant in my opinion. The actual overhead incurred from the number of times you are likely to use this in a view should be negligible. Just my two cents FWIW though.
  • Naveed Abbas
    Naveed Abbas over 6 years
    Can you please explain what is (MyProject). I'm trying to use it in NSObject based class and the syntax is @interface Common : NSObject
  • App Dev Guy
    App Dev Guy over 6 years
    @ToughGuy - it's an extension. The "MyProject" message is somewhat irrelevant but necessary (I don't know why, but you I used (extension) instead) so you can keep it there. Use like so: [UIColor colorForSomePurpose] just like you would red [UIColor red] and etc. [self.view setBackgroundColor: [UIColor colorForSomePurpose] ];