#define directive clarification - objective-c

Sorry if this is a a bad question but . . .
I want to create a shortcut to my UIColor that looks like this
[UIColor colorWithRed:(88.0f/255.0f) green:(201.0f/255.0f) blue:(234.0f/255.0f) alpha:1]
Can I use a #define for that somehow so that I can just type in some shortcut? I've tried but I think my notation may be off . . .
If a #define is only for constants or for some reason doesn't apply in this situation, are there any other ways to shortcut that method?
Thanks!

#define is just a glorified text replacement system. If you define something like this:
#define key value
then the preprocessor will replace every occurrence of keywith value even before the compiler can do anything. So it doesn't really matter what you define. You can use anything as your value, so you could do something like
#define kMyColor [UIColor colorWithRed:(88.0f/255.0f) green:(201.0f/255.0f) blue:(234.0f/255.0f) alpha:1]
But in this case you probably should use a static constant variable:
static const UIColor *myColor = [UIColor colorWithRed:(88.0f/255.0f) green:(201.0f/255.0f) blue:(234.0f/255.0f) alpha:1];

#define RGBA(r,g,b,a) [UIColor colorWithRed:(r) green:(g) blue:(b) alpha:(a)]
Intead using:
[UIColor colorWithRed:(88.0f/255.0f) green:(201.0f/255.0f) blue:(234.0f/255.0f) alpha:1]
Use this:
RGBA(0.35,0.79,0.92,1)
Take a look at this tutorial:
UIColor Shortcuts.

By your question's text it is not really clear, what you want to shortcut — one single color, the creation of RGBA colors with values [0..255]?
DrummerB answered the first, Justin Boo the second.
I want to propose a solution, that fits for both:
Create a category, that can cover both
[UIColor colorWith255ValuesWithRed: 128 green: 35 blue: 40 alpha:255], that wraps the method, you are using now
create a UIColor class method, that stores UIColor objects in a static NSMutableDictionary with their names and the counter part, you would call like [UIColor registeredColorWithName:#"activeForegroundColor"]
I wrote a sample code for the color register idea:
UIColor+Register.h
#import <UIKit/UIKit.h>
#interface UIColor (Register)
+(void)registerColor:(UIColor *)color
forName:(NSString *)name;
+(UIColor *)registeredColorForName:(NSString *)name;
+(void)unregisterColorForName:(NSString *)name;
#end
UIColor+Register.m
#import "UIColor+Register.h"
#interface UIColor (RegisterPrivate)
+(NSMutableDictionary *)colorRegister;
#end
#implementation UIColor (RegisterPrivate)
+(NSMutableDictionary *)colorRegister
{
static dispatch_once_t once;
static NSMutableDictionary *register_;
dispatch_once(&once, ^{
register_ = [NSMutableDictionary dictionary];
});
return register_;
}
#end
#implementation UIColor (Register)
+(void)registerColor:(UIColor *)color
forName:(NSString *)name
{
[[self colorRegister] setObject:color forKey:name];
}
+(UIColor *)registeredColorForName:(NSString *)name
{
return [[self colorRegister] objectForKey:name];
}
+(void)unregisterColorForName:(NSString *)name
{
[[self colorRegister] removeObjectForKey:name];
}
#end
Usage:
register
[UIColor registerColor:[UIColor redColor] forName:#"activeColor"];
[UIColor registerColor:[UIColor grayColor] forName:#"passiveColor"];
access
[view1 setBackgroundColor:[UIColor registeredColorForName:#"passiveColor"]];
[view2 setBackgroundColor:[UIColor registeredColorForName:#"activeColor"]];
unregister
[UIColor unregisterColorForName:#"activeColor"];

Create a category on UIColor and define a new class method (like blackColor, whiteColor etc). This way your code at least fits in with the existing style.
+(UIColor*)myColor
{
return [UIColor colorWithRed:(88.0f/255.0f) green:(201.0f/255.0f) blue:(234.0f/255.0f) alpha:1];
}

Here's a define for HEX RGB if you'd like:
#define UIColorFromRGB(rgbValue) [UIColor \
colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
green:((float)((rgbValue & 0xFF00) >> 8))/255.0 \
blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
Usage:
UIColor *color = UIColorFromRGB(0xe8e600); //0xe8e600 hex representation...

Related

Change UIButton Color With Defined Color

I want to change my title color with an already defined color. Here's my code.
#define GHOSTWHITE_COLOR ([UIColor colorWithRed:248/255.0 green:248/255.0 blue:255/255.0 alpha:1]);
[loginBtn setTitleColor:GHOSTWHITE_COLOR forState:UIControlStateNormal]
But I'm having this error:
Expression result unused** or **Expected ']'
The problem is the semicolon at the end of the #define. Get rid of the semicolon there. Then of course you need to add a semicolon to your regular method call.
#define GHOSTWHITE_COLOR ([UIColor colorWithRed:248/255.0 green:248/255.0 blue:255/255.0 alpha:1])
[loginBtn setTitleColor:GHOSTWHITE_COLOR forState:UIControlStateNormal];
A better solution is to create a category on UIColor and add a class property for your custom color. Then you can use your custom color just like any other.h
UIColor+Custom.h:
#interface UIColor (Custom)
#property (nonatomic, readonly, class) UIColor *ghostWhiteColor;
#end
UIColor+Custom.m:
#implementation UIColor (Custom)
+ (UIColor *)ghostWhiteColor {
return [UIColor colorWithRed:248/255.0 green:248/255.0 blue:255/255.0 alpha:1];
}
#end
Now you can use this like any other color:
[loginBtn setTitleColor:UIColor.ghostWhiteColor forState:UIControlStateNormal];

IOS UITextView category set font

I created a category class which standardize the feel and look of UITextView.
I manage to add border with the code below but not sure how to set font name, font color.
#import "UITextView+Form.h"
#import <QuartzCore/QuartzCore.h>
#implementation UITextView (Form)
-(void)standardize{
CALayer *thisLayer = self.layer;
thisLayer.borderWidth=3.0;
thisLayer.borderColor=[UIColor blackColor].CGColor;
}
#end
This method should work for you:
-(void)standardize{
CALayer *thisLayer = self.layer;
thisLayer.borderWidth=3.0;
thisLayer.borderColor=[UIColor blackColor].CGColor;
// Set whatever point size you want
self.font = [UIFont systemFontOfSize:10.0f];
// Set whatever color you want
self.textColor = [UIColor black];
}
These should help too.
UIFont class reference:
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIFont_Class/Reference/Reference.html
UIColor class reference:
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIColor_Class/Reference/Reference.html
Add these lines to your standardize method:
self.textColor = [UIColor ...]; // whatever color you want
self.font = [UIFont ...]; // whatever font you want

How do I animate an NSColor change?

NSAnimation won't let me animate NSColor changes. How do I do this?
You can use blendedColorWithFraction:ofColor:. In your animation method (whichever method is handling the animation's current value [if you don't have one, simply make one]):
NSColor *startColor = [NSColor redColor];
NSColor *targetColor = [NSColor blueColor];
float progress = [animation currentValue];
NSColor *currentColor = [startColor blendedColorWithFraction:progress ofColor:targetColor];
Edit:
What you can do is create a subclass of NSAnimation. Your subclass just needs to override the setCurrentProgress: method to determine how far along the animation you are. You can configure the rest of the animation exactly the same way. The protocol may be overkill in this scenario but it gives your subclassed animation a dedicated way to give the NSColor back to the class instance that created the animation.
#protocol MyAnimationTarget
- (void) setColorOfSomething:(NSColor *);
#end
#interface MyAnimation : NSAnimation
#property id<MyAnimationTarget> target;
#property NSColor *color1;
#property NSColor *color2;
#end
#implementation MyAnimation
#synthesize target = _target;
#synthesize color1 = _color1;
#synthesize color2 = _color2;
- (void) setCurrentProgress:(NSAnimationProgress) d
{
[super setCurrentProgress:d];
NSColor *currentColor = [self.color1 blendedColorWithFraction:d ofColor:self.color2];
[self.target setColorOfSomething:currentColor];
}
#end
In your other code:
MyAnimation *myAnim = [[MyAnimation alloc] init];
myAnim.target = self; // assuming self has a setColorOfSomething: method
myAnim.color1 = [NSColor redColor];
myAnim.color2 = [NSColor blueColor];
// set up other animation stuff
[myAnim startAnimation];
Just a suggestion. Maybe you can use fade in and fade out like the following:
begin animation
obj.alpha = 0.0;
commit animation
begin animator
obj.color = newColor;
obj.alpha = 1.0;
commit animation

Calling a method within a method (UIColor). Beginner

I am basically trying to make a method that enables me to get random colors for drawing. I have created a method like this:
-(UIColor*)randomColour
{
NSArray *colourArray = [NSArray arrayWithObjects:[UIColor redColor],
[UIColor blueColor],
[UIColor greenColor],nil];
UIColor *colour = [colourArray objectAtIndex:rand()%3];
[colourArray release];
return colour;
}
and I WANT to call it like this
[[UIColor [self randomColour]] setStroke];
or like this
[[UIColor randomColour] setStroke];
but both fails. What is it that I don't understand?
For the second option I get a warning:
"class method '+randomColour' not found"
I have tried reading up on class methods but can't see why it's cussing about it. Any quick pointers?
Thanks
If you have defined randomColour in your own class, you simply do:
[[self randomColour] setStroke];
It should be
[[self randomColour] setStroke];
Try using
+(UIColor*)randomColour
instead of
-(UIColor*)randomColour

How do I define constant values of UIColor?

I want to do something like this, but I cannot get a cooperative syntax.
static const UIColor *colorNavbar = [UIColor colorWithRed: 197.0/255.0 green: 169.0/255.0 blue: 140.0/255.0 alpha: 1.0];
I suppose that I could define macros, but they are ugly.
I like to use categories to extend classes with new methods for this sort of thing. Here's an excerpt of code I just wrote today:
#implementation UIColor (Extensions)
+ (UIColor *)colorWithHueDegrees:(CGFloat)hue saturation:(CGFloat)saturation brightness:(CGFloat)brightness {
return [UIColor colorWithHue:(hue/360) saturation:saturation brightness:brightness alpha:1.0];
}
+ (UIColor *)aquaColor {
return [UIColor colorWithHueDegrees:210 saturation:1.0 brightness:1.0];
}
+ (UIColor *)paleYellowColor {
return [UIColor colorWithHueDegrees:60 saturation:0.2 brightness:1.0];
}
#end
Now in code I can do things like:
self.view.backgroundColor = highlight? [UIColor paleYellowColor] : [UIColor whitecolor];
and my own defined colors fit right in alongside the system-defined ones.
(Incidentally, I am starting to think more in terms of HSB than RGB as I pay more attention to colors.)
UPDATE regarding precomputing the value: My hunch is that it's not worth it. But if you really wanted, you could memoize the values with static variables:
+ (UIColor *)paleYellowColor {
static UIColor *color = nil;
if (!color) color = [UIColor colorWithHueDegrees:60 saturation:0.2 brightness:1.0];
return color;
}
You could make a macro do do the memoizing, too.
I usually make a category of UIColor for each project:
#interface UIColor (ProjectName)
+(UIColor *) colorForSomeTable;
+(UIColor *) colorForSomeControl;
+(UIColor *) colorForSomeText;
#end
With the constants in the implementation:
#implementation UIColor (ProjectName)
+(UIColor *) colorForSomeTable { return [UIColor colorWithRed:...]; }
#end
I also do the same for UIFont and UIImage as needed.
To expand on jasoncrawford's answer (I'd put this in as a comment, but you can't format code in the comments) if you want to precompute the values (or do it only once).
+ (UIColor *)paleYellowColor
{
static UIColor* paleYellow = nil;
if (paleYellow == nil)
{
paleYellow = [UIColor colorWithHueDegrees:60 saturation:0.2 brightness:1.0];
}
return paleYellow;
}
The reason your original idea doesn't work is because the compiler can only use initialisers outside of functions, not normal code. You could have achieved something like what you wanted with the initialize methosd e.g.
static UIColor* colorNavBar = nil;
+(void) initialize
{
if (colorNavBar != nil)
{
colorNavBar = ....
}
}
NB the const qualifier on your original definition is redundant since UIColor is immutable anyway.
You can 'define' a similar CONSTANT like this:
#define FAV_COLOR [UIColor colorWithRed:24/255.0f green:89/255.0f blue:36/255.0f alpha:0.9]
and call it by name like you are used to with constants: FAV_COLOR
Hope that helps.
You can do this:
#define backgroundColorApp [UIColor colorWithRed: 197.0/255.0 green: 169.0/255.0 blue: 140.0/255.0 alpha: 1.0]
In Swift, define an extension:
extension UIColor {
class func XXXWhiteColor() -> UIColor {
return UIColor(red: 256, green: 256, blue: 256, alpha: 1.0)
}
class func XXXGreenColor() -> UIColor {
return UIColor(red: 73/255.0, green: 212/255.0, blue: 86/255.0, alpha: 1.0)
}
}
Use like:
label.background = UIColor.XXXWhiteColor()
#define textColorApp [UIColor colorWithRed: 197.0/255.0 green: 169.0/255.0 blue: 140.0/255.0 alpha: 1.0]
myLabel.textColor=textColorApp;
Just define below macro in your constant file and pass only RGB value and use anywhere you want
#define RGBCOLOR(r,g,b)[UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1]
To use :
[lblCount setTextColor:RGBCOLOR(236, 43, 92)];
I feel it's also worth mentioning another awesome feature that is rarely talked about: Color Literals. Not only are they easier to read, but they are WAY easier to edit. In Swift,
let color: UIColor = #colorLiteral(red: 0.9607843137, green: 0.4784313725, blue: 0.3215686275, alpha:
When pasted into Xcode, this syntax creates a simple color box.
Click here to see an example.
Once you see the box, you can then double click on it to edit it easily. Similarly, you can switch between various IB options, including RGB Sliders, if you have a specific list of color hex values from your designer.