External constants for UIColor, UIFont, etc.

I have a constants.m file, which is a centralized set of many program constants. To set the color, I do this:

@implementation UIColor (UIColor_Constants) +(UIColor *) defaultResultTableBackgroundColor{ //return [[UIColor colorWithRed:0.6f green:0.004f blue:0.0f alpha:1.0f] retain]; return [[UIColor colorWithRed:0.1f green:0.004f blue:0.3f alpha:0.3f] retain]; } +(UIColor *) defaultResultHeaderBackgroundColor{ return [[UIColor clearColor] retain]; } @end 

and in constants. I have

 @interface UIColor (UIColor_Constants) +(UIColor *) defaultResultTableBackgroundColor; +(UIColor *) defaultResultHeaderBackgroundColor; @end 

and then just use [UIColor defaultResultTableBackgroundColor] , where I want to refer to this constant.

I would like to have some other UIColor and UIFont constants, and although this works, it seems more complicated than it should be. Is there an easier way to do this?

+6
source share
5 answers

I also like this way. One question: why do you keep uicolor? It is very dangerous. This can lead to an error and create a memory leak.

+6
source

I use a constant file for the same purpose. Instead of customizing the entire interface and implementation file, I create a constant file as a header (.h) file. Then I define the colors that I want to use, for example:

 #define globalColor [UIColor colorWithRed:0.1f green:0.004f blue:0.3f alpha:0.3f] 

Then, whenever you use globalColor , it is just like input to a specific code.

+8
source

Here's a great read that explains constants in Objective-C:

What is the best way to create constants in Objective-C

Short answer: You do this best.

Macros are not recommended. As already mentioned, you can #define a macro to process your colors. This essentially tells the preprocessor to run find-and-replace in your code. There are a number of disadvantages to this approach, including volume and type. Apple explicitly recommends against these types of "constants": https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CodingGuidelines/Articles/NamingIvarsAndTypes.html

It is also possible to create a constant:

(in your title, content area)

 extern UIColor * const COLOR_LIGHT_BLUE; 

(in your implementation, scope)

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

You could, of course, #import this header in the prefix header to save even more input. Ultimately, this does not greatly improve what you are already doing.

+4
source

You create a file with a permanent header that contains all your colors and / or such fonts:

 // Define a convenient macro for example #define HEXCOLOR(c) [UIColor colorWithRed:((c>>16)&0xFF)/255.0 \ green:((c>>8)&0xFF)/255.0 \ blue:c&0xFF/255.0 \ alpha:0xFF/255.0] // Then define your constants #define DefaultResultTableBackgroundColor HEXCOLOR(0x151515) // etc. 
+3
source

The answer is very simple. It can be used for NSArray and NSDictionary , etc.

 //Font static UIFont *titleFont() { static UIFont *font = nil; if (!font) { font = [UIFont systemFontOfSize:25 weight:UIFontWeightHeavy]; } return font; } //Color static UIColor *prettyPurpleColor() { static UIColor *color = nil; if (!color) { color = [[UIColor purpleColor] colorWithAlphaComponent:0.35]; } return color; } 
0
source

Source: https://habr.com/ru/post/895457/


All Articles