Use the iOS category to create a new color.

I would like to create some new ones UIColorthat will be used in my application. RGB is slightly changed from time to time (the exact color tone is discussed)

Currently, I need to create new colors from RGB, and the code sprinkles and repeats.

Is there a better way where I can create a new color and use it through my application.

[UIColor myNewCustomRedColor]

What is the best example here - this is the category - the right choice - if so, then how? If not, this is the recommended approach.

+5
source share
2 answers
Category

- - . .h/.m, MyCategories.h/MyCategories.m, , .

MyCategories.h:

@interface UIColor (MyCategory)

+ (UIColor *)customRedColor;

@end

MyCategories.m

@implementation UIColor (MyCategory)

+ (UIColor *)customRedColor {
    return [UIColor redColor];
}

@end

.h, , MyApp-Prefix.pch.

+9

, , - ProjectStyle.h, #defines . .

- :

ProjectStyles.h

#define RED_HEADER_COLOR [UIColor colorWithRed:0.8f green:0.1f blue:0.1f alpha:0.9f]
#define RED_BACKGROUND_COLOR [UIColor colorWithRed:0.9f green:0.3f blue:0.1f alpha:1.0f]
#define PRIMARY_FONT [UIFont fontWithName:@"HelveticaNeue-Bold" size:12.0f]

.m , UIColor UIFont, -

ProjectStyles.m

+ (UIColor *) redHeaderColor 
{  return [UIColor colorWithRed:0.8f green:0.1f blue:0.1f alpha:0.9f];  }

+ (UIColor *) redBackgroundColor 
{  return [UIColor colorWithRed:0.9f green:0.3f blue:0.1f alpha:1.0f];  }

+ (UIFont *) primaryFont
{
    static UIFont *font = nil;
    if ( font == nil )
        font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:12.0f];
    return font;
}

, ,

, ProjectStyle, , , - , . , , #import , .

, , ( ) .

+5

All Articles