How to customise UITabBar text label? - ios5

does anybody know how to use the Appearance proxy object
[[UITabBar appearance] set....];
to customise the color,font and shadow for the selected/unselected label of an UITabBar?
thanks a lot.

By accessing the tab-bar item object of the UITabBar for each UIViewController
This only works for iOS5.0 or later.
if ([self.tabBarItem respondsToSelector:#selector(setTitleTextAttributes:)]) {
NSLog(#"*** Support method(iOS 5): setTitleTextAttributes:");
[self.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:#"AmericanTypewriter" size:20.0f], UITextAttributeFont,
[UIColor blackColor], UITextAttributeTextColor,
[UIColor grayColor], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0.0f, 1.0f)], UITextAttributeTextShadowOffset,
nil]];
}
Below 5 https://github.com/boctor/idev-recipes/tree/master/CustomTabBar

Related

UIBarButtonItem appearance iOS5 font

I am using a little chunk of code like below to change the text attributes of my nav bar title app wide and it works great.
[[UINavigationBar appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], UITextAttributeTextColor,
[UIColor grayColor], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, -1)], UITextAttributeTextShadowOffset,
[UIFont fontWithName:#"Cochin-BoldItalic" size:0.0], UITextAttributeFont,
nil]];
But I want to be able to just as easily do this for UIBarButtonItem text as well but I can't figure it out as it doesn't share the same or similar methods it appears.
Edit
Tried this code, not making any changes to text:
[[UIBarItem appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], UITextAttributeTextColor,
[UIColor grayColor], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, -1)], UITextAttributeTextShadowOffset,
[UIFont fontWithName:#"Cochin-BoldItalic" size:12.0], UITextAttributeFont,
nil]
forState:UIControlStateNormal];
You want to use the - (void)setTitleTextAttributes:(NSDictionary *)attributes forState:(UIControlState)state method of UIBarItem (UIBarButtonItem inherits from UIBarItem).
Check out the docs for more details: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIBarItem_Class/Reference/Reference.html#//apple_ref/occ/cl/UIBarItem
Try this:
//Suppose you have initialized barButton elsewhere`
[barButton setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], UITextAttributeTextColor,
[UIColor grayColor], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, -1)], UITextAttributeTextShadowOffset,
[UIFont fontWithName:#"Cochin-BoldItalic" size:12.0], UITextAttributeFont,
nil]
forState:UIControlStateNormal];
This seems to work on iOS 7.1:
[[UIBarButtonItem appearance] setTitleTextAttributes:#{NSForegroundColorAttributeName:[UIColor whiteColor],
NSFontAttributeName:[UIFont fontWithName:#"Resamitz" size:16.0]}
forState:UIControlStateNormal];

How to change the font size of the tab bar controller item name?

I am doing an app based on tabbarController. I have a 3 tabbar items.
My question is: How can I change the font-style for the title on the tab-bar item?
[self.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:#"AmericanTypewriter" size:20.0f], UITextAttributeFont,
[UIColor blackColor], UITextAttributeTextColor,
[UIColor grayColor], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0.0f, 1.0f)], UITextAttributeTextShadowOffset,
nil]];
This will change ur UITabBarItem fonts once and for all throughout the app
For Swift use this in AppDelegate's didFinishLaunching:
Swift 3:
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.blue,NSFontAttributeName: UIFont(name: "Montserrat", size: 11)!], for: .normal)
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.red,NSFontAttributeName: UIFont(name: "Montserrat", size: 11)!], for: .selected)
Sorry, I dont think there's a way to do this. If you're desperate, you'll need to write your own tab bar.
Sadly, this isn't possible currently on iOS unless you build your own custom tab bar, which isn't very difficult with storyboarding on iOS5.
Not possible ,create custome tab bar subclassing UITabbar
If you see this error: 'UITextAttributeTextShadowOffset' is deprecated: first deprecated in iOS 7.0 - Use NSShadowAttributeName with an NSShadow instance as the value.,try this.
NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor grayColor];
shadow.shadowOffset = CGSizeMake(0.0, 0.5);
NSDictionary *attribute = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:#"AmericanTypewriter" size:10.0f], NSFontAttributeName,
[UIColor blackColor], NSForegroundColorAttributeName,
shadow,NSShadowAttributeName,nil];
[[UITabBarItem appearance] setTitleTextAttributes:attribute forState:UIControlStateNormal];
Try this.
[[UITabBarItem appearanceWhenContainedIn:[UITabBar class], nil]
setTitleTextAttributes:#{NSForegroundColorAttributeName:
[UIColor colorWithRed:0/255.0f green:130/255.0f blue:202/255.0f alpha:1.0],
NSFontAttributeName:[UIFont fontWithName:#"Signika-Semibold" size:20.0]
}
forState:UIControlStateNormal];

Change UILabel Text inside UISegmentedControll according to its status

Is there any way to use UIAppearance to change the text color of a selected segment in UISegmented Controller and let other segments have same Color ?
NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor colorWithRed:220.0/255.0 green:104.0/255.0 blue:1.0/255.0 alpha:1.0], UITextAttributeTextColor,
[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, 1)], UITextAttributeTextShadowOffset,
[UIFont systemFontOfSize:14], UITextAttributeFont,
nil];
[[UISegmentedControl appearance] setTitleTextAttributes:attributesDictionary forState:UIControlStateSelected];
Docs:
UIAppearance Protocol Reference
setTitleTextAttributes:forState:

How to remove gradient / drop shadow for UITabbaritem in iOS 5

with iOS 5, there is UI Appearance, is it possible to remove the gradient easy way ?
i do this to customised my tabbar, what can be done to remove the gradient ?
thanks for reading
-(void)UIAppearances
{
//set the background of tab bar
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"navbar_bgrd.png"]];
if ([[[UIDevice currentDevice] systemVersion] floatValue] > 4.9) {
//iOS 5
//[self.tabBarController.tabBar insertSubview:imageView atIndex:1];
[[UITabBar appearance] setBackgroundImage:[UIImage imageNamed:#"navbar_bgrd.png"]];
[[UITabBar appearance] setSelectionIndicatorImage:
[UIImage imageNamed:#"navbaractive.png"]];
[[UITabBarItem appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor colorWithRed:75.0/255.0 green:75.0/255.0 blue:75.0/255.0 alpha:1.0], UITextAttributeTextColor,
[UIColor clearColor], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, 1)], UITextAttributeTextShadowOffset,
nil] forState:UIControlStateNormal];
[[UITabBarItem appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], UITextAttributeTextColor,
[UIColor clearColor], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, 1)], UITextAttributeTextShadowOffset,
nil] forState:UIControlStateSelected];
//nav bar
[[UINavigationBar appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:#"Rokkitt" size:28.0],
UITextAttributeFont,
nil]];
}
else {
//iOS 4.whatever and below
[self.tabBarController.tabBar insertSubview:imageView atIndex:0];
}
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque animated:NO];
}
UITabBar can either take an image and use its alpha channel (opacity) to build the selected/unselected images or take two processed images to use as is.
You'll have to provide the images yourself to the UITabBarItem with setFinishedSelectedImage:withFinishedUnselectedImage:. There's no other way to affect the processing it does to the images besides changing the color of the gradient with UITabBar's selectedImageTintColor appearance property.

iPhone UINavigationBar change font style for all controllers with [UINavigationBar appearance]

I'm aware that I can individually change the font of a navigation bar as outlined in this answer: Change the navigation bar's font
Currently I'm using a more global approach:
//in my app delegate:
[[UINavigationBar appearance] setBarStyle:UIBarStyleBlackTranslucent];
Is there a way to globally change the font that the Navbar through the appearance object?
thank you!
From Ray Wenderlich:
http://www.raywenderlich.com/4344/user-interface-customization-in-ios-5
// Customize the title text for *all* UINavigationBars
[[UINavigationBar appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0],
UITextAttributeTextColor,
[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8],
UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, -1)],
UITextAttributeTextShadowOffset,
[UIFont fontWithName:#"Arial-Bold" size:0.0],
UITextAttributeFont,
nil]];
#Javy's answer with #Philip007's suggestion:
[[UINavigationBar appearance] setTitleTextAttributes: #{
UITextAttributeTextColor: [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0],
UITextAttributeTextShadowColor: [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8],
UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetMake(0.0f, 1.0f)],
UITextAttributeFont: [UIFont fontWithName:#"Helvetica-Light" size:0.0f]
}];
ahh... that's better!
Above answers with updates for deprecated keys and use of NSShadow:
NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor blackColor];
shadow.shadowBlurRadius = 0.0;
shadow.shadowOffset = CGSizeMake(0.0, 2.0);
[[UINavigationBar appearance] setTitleTextAttributes: #{
NSForegroundColorAttributeName : [UIColor blackColor],
NSFontAttributeName : [UIFont fontWithName:#"Helvetica-Light" size:0.0f],
NSShadowAttributeName : shadow
}];
Also setting the font size to 0 so it automatically resizes based on navigation bar orientation/height.