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];
Related
I have this code should modify the colors of 24 buttons, connected to a IBoutletCollection :
In .h File:
#property (nonatomic, strong) IBOutletCollection(UIButton) NSArray *buttons;
In .m File:
for (UIButton *label in buttons) {
label.layer.borderWidth = 1.5f;
label.layer.borderColor = (__bridge CGColorRef)([UIColor colorWithRed:87.0/255.0 green:49.0/255.0 blue:42.0/255.0 alpha:1]);
label.layer.backgroundColor = (__bridge CGColorRef)([UIColor colorWithRed:134.0/255.0 green:98.0/255.0 blue:98.0/255.0 alpha:1]);
}
The IBOutletCollection's connected to the existing 24 buttons in my view, which is initially in gray color, when I run this function to modify the colors of the buttons they all go to the white color, how can I solve this?
use like this
[[myButton layer] setBorderWidth:2.0f];
[[myButton layer] setBorderColor:[UIColor colorWithRed:87.0/255.0 green:49.0/255.0 blue:42.0/255.0 alpha:1].CGColor];
[myButton setTitleColor:[UIColor colorWithRed:150.0/256.0 green:150.0/256.0 blue:150.0/256.0 alpha:1.0]]
I'm trying to create a function to change the size of something like a UILabel or UIButton without having to type the three lines out every time. This is what I have.
-(void)setObject:(UIControl*)object SizeWidth:(NSInteger)width Height:(NSInteger)height
{
CGRect labelFrame = object.frame;
labelFrame.size = CGSizeMake(width, height);
object.frame = labelFrame;
}
However, when I give (UIControl*)object a UILabel, it says "incompatible pointer types". How can I fix this to work for anything I can put on a UIView?
UILabel is not a subclass of UIControl, it inherits from UIView.
Try changing UIControl to UIView:
-(void)setObject:(UIView*)object SizeWidth:(NSInteger)width Height:(NSInteger)height
{
CGRect labelFrame = object.frame;
labelFrame.size = CGSizeMake(width, height);
object.frame = labelFrame;
}
(UIControl inherits from UIView anyway, and frame is a UIView property)
Label is not a subclass of UIControl. You can use UIView in place of UIControl.
Here is the hierarchy for UILabel
UILabel: UIView : UIResponder : NSObject
-(void)setObject:(UIView*)object SizeWidth:(NSInteger)width Height:(NSInteger)height
{
CGRect labelFrame = object.frame;
labelFrame.size = CGSizeMake(width, height);
object.frame = labelFrame;
}
One suggestion for you is, the method name seems kind of odd to me. You can write a simple category to update the size for UIView. With the following category you can simply call
[myLabel setWidth:20 andHeight:20];
In UIView + MyCategory.h
#import <UIKit/UIKit.h>
#interface UIView (MyCategory)
- (void)setWidth:(NSInteger)aWidth andHeight:(NSInteger)aHeight;
#end
In UIView + MyCategory.m
#import "UIView + MyCategory.h"
#implementation UIView (MyCategory)
- (void)setWidth:(NSInteger)aWidth andHeight:(NSInteger)aHeight;
{
CGRect frameToUpdate = self.frame;
frameToUpdate.size = CGSizeMake(aWidth, aHeight);
self.frame = frameToUpdate;
}
#end
And to address the actual problem you're trying to solve, I highly recommend using this set of helpers https://github.com/kreeger/BDKGeometry
Following Obj-C style conventions (as selecting the right tools for the job) make it more efficient for others to read and comprehend our code. The Objective-C style here needs a bit of cleanup. See my notes after the source if you're interested. On to a more concise way to do this:
You can go the class method route (perhaps in a view manipulation class)
#implementation CCViewGeometry
+ (void)adjustView:(UIView *)view toSize:(CGSize)size
{
CGRect frame = view.frame;
frame.size = size;
view.frame = frame;
}
#end
or the UIView category route
#implementation UIView (CCGeometry)
- (void)resize:(CGSize)size
{
CGRect frame = self.frame;
frame.size = size;
self.frame = frame;
}
#end
Style notes pertinent to code found on this page:
All method params should begin with a lower-case char.
setFoo: is used in #property synthesis & by convention your method name indicates setting a property named object to the value of object. You're setting the size, not the object itself.
Be explicit. Why have a method called setObject: when you know the general type of object being passed?
Width & height in UIKit are represented (rightly) by CGFloat, not NSInteger. Why pass width + height instead of CGSize anyway?
Try using class methods when state is not required. + is your friend. Don't fire up instances for every little thing (most singleton methods I see in ObjC code should be refactored as class methods).
The programmers that don't care about the little things end up with unmaintainable code—code that will slow them and those that come after them down. Convention and style matter a lot on any decent-sized project.
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
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
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...