iOS setSelectedImageTintColor uicolor not accepted - iphone

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]];

Related

How to use this method

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];

How do you change font in UISearchBar for iOS5?

I'm trying to change the font for all of my UISearchBar objects using the new "appearance" proxy for iOS5 with something like:
[[UISearchBar appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], UITextAttributeTextColor,
[UIFont fontWithName:#"Trebuchet MS" size:0.0], UITextAttributeFont,
nil]];
Every time I run this, I'm getting this error:
"2012-05-28 03:01:52.264 DirectDx_ClientApp[30039:15503] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_UIAppearance setTitleTextAttributes:]: unrecognized selector sent to instance 0x8460cf0'
* First throw call stack:
(0x1ea8022 0x3a4fcd6 0x1ea9cbd 0x1e0eed0 0x1e0ecb2 0x5e695 0x5dd78 0x217aa 0x789386 0x78a274 0x799183 0x799c38 0x78d634 0x203bef5 0x1e7c195 0x1de0ff2 0x1ddf8da 0x1dded84 0x1ddec9b 0x789c65 0x78b626 0x2164d 0x2895 0x1)
terminate called throwing an exception"
The method above works perfectly well with UITabBar and UINavigationBar.
Any insights?
Thanks very much in advance.
Try this,It's Working Fine for iOS 5.0 and up (iOS 7 also):
- (void)viewDidLoad
{
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setFont:[UIFont fontWithName:#"Helvetica" size:20]];
}
For iOS 8
- (void)viewDidLoad
{
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setDefaultTextAttributes:#{
NSFontAttributeName: [UIFont fontWithName:#"Helvetica" size:20],
}];
}
Checkout the apple reference guide for uisearchbar, under customizing appearance you can see what methods are avialable to you for a uisearchbar. SetTitleAttributes is not possible for a uisearchbar and I can not see another method where you change the font of the uisearchbar by means of appearance.
Trying to call a method that is not supported by an object, will always give you an error. In your case setTitleArtributes is supported by other classes but sadly enough not for the uisearchbar.
There is a workaround.
Try using this code
UITextField *textField = [self.searchBar valueForKey: #"_searchField"];
[textField setFont:[UIFont fontWithName:#"HelveticaNeue-Light" size:17.0]];
Hope this will work for u.

how can I pass UIColor's name

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.

Cannot create a UIColor using colorFromPatternImage:

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"]];

Objective-C use typedef enum to set Class behavior, like Cocoa

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.