UITabBarItem appearance for UIControlStateHighlighted - ios

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

Related

Changing the font in a UITabBar

I was wondering if someone would be able to help me with a problem I am having at the moment. I a m trying to change the actual font of the UITabBar, I have been able to chance the text colour and the colour when the text is selected. The code i have so far is here:
//Setting tab bar background
UIImage* tabBarBackground = [UIImage imageNamed:#"tabbar.png"];
[[UITabBar appearance] setBackgroundImage:tabBarBackground];
//[[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:#"selected.png"]];
// Changes Selected Image color
[[UITabBar appearance] setTintColor:[UIColor colorWithRed:33.0/255.0 green:35.0/255.0 blue:38.0/255.0 alpha:1.0]]; //Icon colour
// Changes StateNormal text Color,
[UITabBarItem.appearance setTitleTextAttributes: #{NSForegroundColorAttributeName : [UIColor colorWithRed:214.0/255 green:69.0/255 blue:65.0/255 alpha:1.0]} forState:UIControlStateNormal]; //Colour of text when not seleted
// Changes StateSelected
UIColor *titleHighlightedColor = [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0]; //Colour of text when selected
[UITabBarItem.appearance setTitleTextAttributes: #{NSForegroundColorAttributeName : titleHighlightedColor} forState:UIControlStateSelected];
I was wondering if anyone would know if it is possible to change the font here and if so how i would come across doing so? The code is located in the AppDelegate.m
Thanks in advance,
Harrison
Try adding it to the title text attributes:
[UITabBarItem.appearance setTitleTextAttributes: #{
NSForegroundColorAttributeName : [UIColor colorWithRed:214.0/255 green:69.0/255 blue:65.0/255 alpha:1.0],
NSFontAttributeName : [UIFont systemFontOfSize:20]
} forState:UIControlStateNormal];
You could use
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:#"Arial" size:5.0f], NSFontAttributeName, nil] forState:UIControlStateNormal];

Is there a way to change title in navigation bar to Italic, Bold and underlined without changing font?

I use the UIAppearance to change attributes of my title in Navigation Bar like so:
[[UINavigationBar appearance] setTitleTextAttributes:#{NSForegroundColorAttributeName : [MM mainTitleColor]}];
But I have not found the way to make text underlined or italic, is there a way to do this without changing the font alltogether?
No. These properties you cant change unless font changing. Because the available keys under the appearance proxy are
UITextAttributeFont
UITextAttributeTextColor
UITextAttributeTextShadowColor
UITextAttributeTextShadowOffset
Change these properties to customize the UINavigationBar
If you looking for font change, then see the below example
[[UINavigationBar appearance] setTitleTextAttributes:#{
UITextAttributeTextColor: TEXT_COLOR,
UITextAttributeTextShadowColor: SHADOW_COLOR,
UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetMake(0, -1)],
UITextAttributeFont: [UIFont fontWithName:#"Arial-Bold" size:0.0],
}];
Try this one.
You have to customise your navigation controller with adding a imageview.
UIFont *yourFont = [UIFont fontWithName:#"Helvetica-BoldOblique" size:[UIFont systemFontSize]];
if ([[UINavigationBar class]respondsToSelector:#selector(appearance)]) {
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"myimage.png"] forBarMetrics:UIBarMetricsDefault];
}
[[UINavigationBar appearance] setTitleTextAttributes:#{
UITextAttributeTextColor : [UIColor clearColor],
UITextAttributeTextShadowColor : [UIColor blackColor],
UITextAttributeTextShadowOffset : [NSValue valueWithUIOffset:UIOffsetMake(1, 0)],
UITextAttributeFont : yourFont
}];
You can use :
Optima-BoldItalic
TimesNewRomanPS-BoldItalicMT
Baskerville-BoldItalic
HelveticaNeue-BoldItalic
TrebuchetMS-Bold
Helvetica-BoldOblique
You can try below code to make your text italic and bold.
NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8];
shadow.shadowOffset = CGSizeMake(0, 1);
[[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1.0], NSForegroundColorAttributeName,
shadow, NSShadowAttributeName,
[UIFont fontWithName:#"HelveticaNeue-BoldItalic" size:21.0], NSFontAttributeName, nil]];
[self setTitle:#"Title text"];

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

iOS 5: tabBarItem setTitleTextAttributes for selected vs unselected state

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?

change tabbar text color,iPhone

I have created a tabbar programmatically.
Can we change color of title of the tabbar item?
Default is white, and i m trying to make it black.
something like
tabbaritem.text.textcolor=[UIcolor Blackcolor];
Thanks
In iOS5 you use the appearance proxy to change the title color:
For a specific tabbar item:
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:#"AmericanTypewriter" size:20.0f], UITextAttributeFont,
[UIColor yellowColor], UITextAttributeTextColor,
[UIColor redColor], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0.0f, 1.0f)], UITextAttributeTextShadowOffset,
nil] forState:UIControlStateNormal];
Or replace self.tabBarItem with [UITabBarItem appearance] to change all your tabbar items.
If you want to change them all , I suggest you place the code on appdelegate didFinishLaunchingWithOptions:
Just something else ...
To set your standard appearance in iOS 5:
In your AppDelegate.m do:
[[UITabBar appearance] setTintColor:myColor]; //or whatever you want to change
Saves you a lot of work.
Maybe this updated code helps someone:
[UITabBarItem.appearance setTitleTextAttributes:#{
NSForegroundColorAttributeName : [UIColor lightGrayColor] } forState:UIControlStateNormal];
[UITabBarItem.appearance setTitleTextAttributes:#{
NSForegroundColorAttributeName : [UIColor whiteColor] } forState:UIControlStateSelected];

Resources