iOS 5: tabBarItem setTitleTextAttributes for selected vs unselected state - ios

The attributes set for UITabBarItem UIControlStateNormal overrides of UIControlStateSelected!
[navigationCtrl.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [Util colorFromHex:#"474747"], UITextAttributeTextColor, [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0], UITextAttributeTextShadowColor, [NSValue valueWithUIOffset:UIOffsetMake(1.0, 1.0)], UITextAttributeTextShadowOffset, nil] forState:UIControlStateNormal];
[navigationCtrl.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [Util colorFromHex:#"FCFCFC"],UITextAttributeTextColor, nil] forState:UIControlStateSelected];
Why?

Related

Change color of inactive tab-bar icons

I try to change color of my tab bar items, because it always grey in unactive and blue in active.
So, after some searching I try to write this code it all my ViewControllers for Tab bar
self.tabBarItem.selectedImage = [[UIImage imageNamed:#"TabBarItemMenu_tabbed.png"]
imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
self.tabBarItem.image = [[UIImage imageNamed:#"TabBarItemMenu.png"]
imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
But it doesn't help me, and I always get
You can do this in addition:
Set tintColor attribute of the tab bar to set the color of the selected icon
self.tabBar.tintColor = [UIColor redColor];
Then you can use text attributes to recolor the text
for (UITabBarItem *item in self.tabBar.items) {
NSDictionary *normalState = #{UITextAttributeTextColor : [UIColor colorWithWhite:1.000 alpha:1.000],
UITextAttributeTextShadowColor: [UIColor clearColor],
UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetMake(0.0, 1.0)]};
[item setTitleTextAttributes:normalState forState:UIControlStateNormal];
NSDictionary *selectedState = #{UITextAttributeTextColor : [UIColor redColor],
UITextAttributeTextShadowColor: [UIColor clearColor],
UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetMake(0.0, 1.0)]};
[item setTitleTextAttributes:selectedState forState:UIControlStateHighlighted];
}
// Edit
Since upper code is deprecated for iOS7, here an update:
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], NSForegroundColorAttributeName,
nil] forState:UIControlStateNormal];
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor redColor], NSForegroundColorAttributeName,
nil] forState:UIControlStateSelected];

UINavigationBar needs to be transparent

I have got UINavigationBar styling set in AppDelegate but it doesn't seem to be taking effect on my app. I am using XCode 5 and my code is as follows:
- (void) customizeApp
{
UIImage* menuBackground = [UIImage imageNamed:#"menu-bar.png"];
[[UINavigationBar appearance] setBackgroundImage:menuBackground forBarMetrics:UIBarMetricsDefault];
// 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:#"HelveticaNeue-Light" size:0.0],
UITextAttributeFont,
nil]];
}
Im not sure what Im doing wrong and why its not working. This was working in XCode 4/iOS6
[self.navigationController.navigationBar setBackgroundImage:menuBackground forBarMetrics:
UIBarMetricsDefault];

UITabBarItem appearance for UIControlStateHighlighted

I have this code in my AppDelegate.m file under didFinishLaunchingWithOptions: so that I can have a custom font.
[[UITabBarItem appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor grayColor], UITextAttributeTextColor,
[UIFont fontWithName:#"Gotham-Medium" size:10], UITextAttributeFont, nil] forState:UIControlStateNormal];
[[UITabBarItem appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], UITextAttributeTextColor,
[UIFont fontWithName:#"Gotham-Medium" size:10], UITextAttributeFont, nil] forState:UIControlStateHighlighted];
The font change works as expected, but I get this output in the Debug console:
button text attributes only respected for UIControlStateNormal, UIControlStateHighlighted and UIControlStateDisabled. state = 1 is interpreted as UIControlStateHighlighted.
Change UIControlStateHighlighted to UIControlStateSelected

Why does UISegementedControl's setTitleTextAttributes only change the text color but not the font face/weight for active state?

With the following code I try to change the appearance of a UISegmentedControl, but only the text color changes with highlighted or selected tabs. Font face and weight are taken from the inactive attributes, so that all parts of the segmented control show Futura-Medium on the title label, no matter if highlighted, selected or normal. Why is this?
NSDictionary *attributesActive = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:#"AmericanTypewriter-Bold" size:22.f], UITextAttributeFont,
[UIColor redColor], UITextAttributeTextColor,
[UIColor clearColor], UITextAttributeTextShadowColor,
nil];
NSDictionary *attributesInactive = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:#"Futura-Medium" size:16.f], UITextAttributeFont,
[UIColor greenColor], UITextAttributeTextColor,
[UIColor clearColor], UITextAttributeTextShadowColor,
nil];
[segmentedControl setTitleTextAttributes:attributesActive forState:UIControlStateHighlighted];
[segmentedControl setTitleTextAttributes:attributesActive forState:UIControlStateSelected];
[segmentedControl setTitleTextAttributes:attributesInactive forState:UIControlStateNormal];

Vertically centering font in Segmented Controller

For some reason, the font in my segmented control is not vertically centered. Is there a way to move the font up to be centered vertically?
The only settings I'm putting on them are below (except for declaring the active/inactive/divider images):
NSDictionary *normalSegmentedControlAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor blackColor], UITextAttributeTextColor,
[UIColor clearColor], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, 1)], UITextAttributeTextShadowOffset,
[UIFont fontWithName:kFont size:kSegConFontSize], UITextAttributeFont,
nil];
[[UISegmentedControl appearance] setTitleTextAttributes:normalSegmentedControlAttributes forState:UIControlStateNormal];
NSDictionary *selectedSegmentedControlAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], UITextAttributeTextColor,
[UIColor clearColor], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, 1)], UITextAttributeTextShadowOffset,
[UIFont fontWithName:kFont size:kSegConFontSize], UITextAttributeFont,
nil];
[[UISegmentedControl appearance] setTitleTextAttributes:selectedSegmentedControlAttributes forState:UIControlStateSelected];

Resources