I want to use one method for setting the textField.layer properties in one method in utility class. And want to call that method from viewController.
// Utility class
+(void) createRoundedBorderToView:(UIView*)view withColor:(UIColor*)color withCornerRadius:(float)cornerRadius withBorderWidth:(int)borderWidth{
view.layer.borderColor = [color CGColor];
view.layer.borderWidth = borderWidth;
view.layer.cornerRadius = cornerRadius;
}
But not getting how to call the method. I tried below, but giving error.
CGFloat cornerRadius = 5.0f;
[Utilities createRoundedBorderToView:_usernameTextField withColor:[UIColor clearColor] withCornerRadius:cornerRadius];
It is giving crash.
[Utilities createRoundedBorderToView:withColor:withCornerRadius:]: unrecognized selector sent to class 0xdbf80
+(void) createRoundedBorderToView:(UIView*)view withColor:(UIColor*)color withCornerRadius:(float)cornerRadius withBorderWidth:(int)borderWidth{
}
In above method to call you need to pass three parameters.
Like:-
[Utilities createRoundedBorderToView:_usernameTextField withColor:[UIColor clearColor] withCornerRadius:cornerRadius withBorderWidth:10];
are you missed param withBorderWidth:(int)borderWidth
you should call for example:
[Utilities createRoundedBorderToView:_usernameTextField withColor:[UIColor clearColor] withCornerRadius:cornerRadius withBorderWidth:1];
Related
Following method is accepted if I set UIColor like:
[tabBarController.tabBar setSelectedImageTintColor:[UIColor greenColor]];
However I would like to call it like:
[tabBarController.tabBar setSelectedImageTintColor:colors];
when
- (UIColor *)colors{
UIColor *colorIcon = [UIColor greenColor];
return colorIcon;
}
and program returns an error "undeclared identifier colors". What am I doing wrong? Thanks
Try this instead:
[tabBarController.tabBar setSelectedImageTintColor:[self colors]];
I want to know that how can I pass UIColor's name to the specific method ?
EDIT :
- (id) setLabel:(NSString *)text WithColorName:(NSString *)nameOfColor FontName:(NSString *)f_name FontSize:(float)f_size abel:(UILabel *)templbl
{
templbl.backgroundColor = [UIColor ?????];
return templbl;
}
any suggestions ?
Thanks..
NSString *colorStr = #"magenta";
NSString *selectorString = [colorStr stringByAppendingString:#"Color"];
SEL selector = NSSelectorFromString(selectorString);
UIColor *color = [UIColor blackColor];
if ([UIColor respondsToSelector:selector]) {
color = [UIColor performSelector:selector];
}
- (id) setLabel:(NSString *)text WithColorName:(NSString *)nameOfColor FontName:(NSString *)f_name FontSize:(float)f_size abel:(UILabel *)templbl color:(UIColor*) myLabelColor
{
templbl.backgroundColor = myLabelColor;
return templbl;
}
one option is a dictionary, where the name is the key and the color is the value
You can dynamically invoke the color class methods:
- (id) setLabel:(NSString *)text WithColorName:(NSString *)nameOfColor FontName:(NSString *)f_name FontSize:(float)f_size abel:(UILabel *)templbl
{
SEL colorMethod = NSSelectorFromString([NSString stringWithFormat:#"%#Color", [nameOfColor lowercaseString]]);
// Check if this is a valid color first
if ([[UIColor class] respondsToSelector:colorMethod]) {
// Dynamically invoke the class method
UIColor *color = [[UIColor class] performSelector:colorMethod];
templbl.backgroundColor = color;
}
}
Jhaliya's answer will work, it's not actually strictly what you were asking (in your example you don't want to pass a UIColor as a parameter, you just want to pass it's preset string name.
This is a little tricky, since things like [UIColor redColor] are methods, not string parameters. You would have to use NSSelectorFromString to achieve it. Much better to pass a UIColor in as Jhaliya's answer shows.
The following works without issue:
toolBar.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"window_bkd.png"]];
However, I have similar statements scattered throughout my code and wanted to clean it up using the following statements, which crash on executing the first statement:
UIColor *bkdColor = [[UIColor alloc] colorWithPatternImage:[UIImage imageNamed:#"window_bkd.png"]];
toolBar.backgroundColor = bkdColor;
[bkdColor release];
Console output from the crash:
[UIPlaceholderColor colorWithPatternImage:]: unrecognized selector sent to instance 0x5203c90
Thanks for your help, I'm sure this is a Homer Simpson "doh!" mistake.
You accidently placed an alloc call in your second version, so you are calling colorWithPatternImage on an instance, while it is a class method. Doh! :-)
This is how it's done correctly:
UIColor *bkdColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"window_bkd.png"]];
What I want is to override UINavigationBar tintColor setter and force default color to it. Bellow is my code (which of course fails). Is there a way to make it work?
#implementation UINavigationBar (UINavigationBarCategory)
- (void)setTintColor:(UIColor *)tint {
self.tintColor = [UIColor greenColor];
}
#end
(I'm using property as a generic term here, in your example it's a stand-in for tintColor)
I don't think you can use the self.property = syntax for assignment inside a setProperty: method. self.property is just an alias for [self setProperty:<value>] and it will recursively call itself.
You'll have to do something like this:
- (void)setProperty:(id)pProperty {
[property autorelease];
property = [pProperty retain];
}
I'm still not 100% sure what you're trying to do will work, but the preceding is a start.
If you want to force a default color of a NavigationBar, why don't you set the tint color in viewDidLoad/viewDidAppear of your view controller??
self.navigationController.navigationBar.tintColor = [UIColor (color you want)];
Im extending the UIButton Class to be able to set the font and color of the UINavigationBarButton ( from this code example: switch on the code )
I goes like this:
#interface NavBarButtonGrey : UIButton
-(id)init;
#end
#implementation NavBarButtonGrey
-(id)init {
if(self = [super init]) {
self.frame = CGRectMake(0, 0, 49.0, 30.0);
self.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
UIImage *image = [UIImage imageNamed:#"greyNavButton.png"];
UIImage *stretchImage =
[image stretchableImageWithLeftCapWidth:15.0 topCapHeight:0.0];
[self setBackgroundImage:stretchImage forState:UIControlStateNormal];
self.backgroundColor = [UIColor clearColor];
[self setTitleShadowColor:[UIColor blackColor] forState:UIControlStateNormal];
self.titleShadowOffset = CGSizeMake(0, -1);
self.titleLabel.font = [UIFont boldSystemFontOfSize:13];
}
return self;
}
#end
This is ok, but of course not very flexible.
How do I incorporate using a typedef enum (like Apple does) for all the different
colors, fonts and sizes I would like my custom button to conform to.
The only thing I can get out of the interface files from UIKit is that it is done like this:
typedef enum {
RGCustomNavBarButtonStyleBlue,
RGCustomNavBarButtonStyleGrey,
RGCustomNavBarButtonStyleBlack,
RGCustomNavBarButtonStyleGreen,
RGCustomNavBarButtonStyleRed,
} RGCustomNavBarButtonStyle;
How to get from that and into a working implementation that takes font, size, color etc. from the values of the enum through the constructor(initWithStyle)?
Does one overload constructors in Objective C? multiple constructors?
Hope it makes sense and thank you for any help given:)
To expand on what ennuikiller said above, I was taught (Hillegass's book) to pick one initializer—usually the one with the most options, like your initWithFont:andColor:—and have the other initializers call it. That main initializer is referred to as the designated initializer.
So your code would have a fully-implemented initWithFont:andColor: that calls [super init], and then you'd also have an initWithFont: that looks something like this:
-(MyClass) initWithFont: (UIFont) font
{
[self initWithFont:font andColor:RGCustomNavBarButtonStyleBlack];
}
Then your initWithFont:andColor: would handle all the other setup and calling [super init].
You can have multiple constructors such as;
-(MyClass) initWithFont: (UIFont) font;
-(MyClass) initWithFonmt: (UIFont) font andColor: (UIColor) color;
etc.
Then call [super init] as the first line in each of your custom constructors.