Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective C defining UIColor constants

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!

like image 922
futureelite7 Avatar asked May 13 '10 02:05

futureelite7


People also ask

What is CGColor in Swift?

CGColor is the fundamental data type used internally by Core Graphics to represent colors. CGColor objects, and the functions that operate on them, provide a fast and convenient way of managing and setting colors directly, especially colors that are reused (such as black for text).

What is Alpha in UIColor?

alpha. The opacity value of the new color object, specified as a value from 0.0 to 1.0.

What happens when color or UIColor has values outside 0 1?

Suggested approach: In the old days values outside of 0 and 1 would be clamped (i.e., forced to 0 or 1) because they didn't mean anything, but wide color support means that is no longer the case – a red value beyond 1.0 is especially red, going into the Display P3 gamut.

How do I make my own UIColor?

Creating a custom UIColor Object This can be done in swift by instantiating a custom UIColor object with an initializer. These are some available initializers, init(white: CGFloat, alpha: CGFloat) init(hue: CGFloat, saturation: CGFloat, brightness: CGFloat, alpha: CGFloat)


1 Answers

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 
like image 102
drawnonward Avatar answered Oct 05 '22 06:10

drawnonward