Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define a global color

I want to define a global colour that I can reuse for a downstate for various custom ui cells

Not sure if this is the correct way to do this but..

I've defined a Class called lightGreyUIColor which has this .h file -

#import <UIKit/UIKit.h>

@interface lightGreyUIColor : UIColor
+ (UIColor*)lightGreyBGColor;

@end

and this . m file -

#import "lightGreyUIColor.h"

@implementation lightGreyUIColor

+ (UIColor*)lightGreyBGColor {
return [UIColor colorWithRed:241.0/255.0 green:241/255.0 blue:241/255.0 alpha:1];
}

@end

I have included the lightGreyUIColor.h file in the implementation file for the tableview and tried to reference it as folows -

        cell.backgroundColor = [UIColor lightGreyBGColor];

Which just produces a no known class or method error for lightgreyBGColor, where am I going wrong and is there a better way to implement a global style than this?

like image 295
Dancer Avatar asked Nov 21 '13 12:11

Dancer


People also ask

How do I define a global color variable in CSS?

Global variables can be accessed/used through the entire document, while local variables can be used only inside the selector where it is declared. To create a variable with global scope, declare it inside the :root selector. The :root selector matches the document's root element.

What are global values in CSS?

Global variables are generic variables that will be used to keep the consistency between all our components. Some examples of global variables are font, default font-size and color palette. It's super simple to define global variables on CSS, we use the :root selector and inside it, we define our variables.

What do the Colours mean in globle?

The darker the shade, the closer you are to the right country. In the above image, you can tell that Italy is closer to the country of the day than Turkey or Pakistan, since it's a darker shade. Globle also offers a color-blind mode with high-contrast colors for those who need or prefer it.

What is global style in CSS?

What are global styles? When people talk about the global nature of CSS, they can mean one of a few different things. They may be referring to rules on the :root or <body> elements that are inherited globally (with just a few exceptions).


2 Answers

You should create a category, not a subclass. This will extend the UIColor class, and add your colors to it.

.h

#import <UIKit/UIKit.h>

@interface UIColor (CustomColors)

+ (UIColor *)myColorLightGreyBGColor;

@end

.m

#import "UIColor+CustomColors.h"

@implementation UIColor (CustomColors)



+ (UIColor *)myColorLightGreyBGColor {

    static UIColor *lightGreyBGColor;

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        lightGreyBGColor = [UIColor colorWithRed:241.0 / 255.0 
                                           green:241.0 / 255.0
                                            blue:241.0 / 255.0 
                                           alpha:1.0];
    });

    return lightGreyBGColor;
}

@end

By defining your colors this way, and #importing the category, you can apply this custom color the way you were already trying to.

like image 67
Mick MacCallum Avatar answered Sep 20 '22 14:09

Mick MacCallum


How about a macro?

#define DEFAULT_COLOR_BLUE [UIColor colorWithRed:.196 green:0.3098 blue:0.52 alpha:1.0]

Put it in your appname_Prefix.pch file or more likely a header file included in your prefix file

And it will be like:

cell.backgroundColor = DEFAULT_COLOR_BLUE;
like image 43
Laszlo Avatar answered Sep 18 '22 14:09

Laszlo