Title text color in bar button item - ios

I have a bar button item that only functions to display a string that is updated when something is performed on screen. I have set the text color to white. But, it is displayed on screen with a gray color. Whatever color I change the text to I still get a grayish color instead of the desired color. Why am I not getting the the right color? Is it a property that I'm missing?
UIColor *buttonColor = [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0];
_timeButton.title = [NSString stringWithFormat:#"Updated at: %#",dateString];
[_timeButton setStyle:UIBarButtonItemStylePlain];
_timeButton.tintColor = [UIColor clearColor];
[_timeButton setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont boldSystemFontOfSize:15], UITextAttributeFont,
buttonColor ,UITextAttributeTextColor,
nil]
forState:UIControlStateNormal];

Turns out that my text color of my button was gray because the button was not enabled. Setting the button to enabled changed the color of the text to white.
[_timeButton setEnabled:YES];

Related

Setting the stroke color of the title text in UINavigationBar's appearance

Is there some problem setting a nav bar's title text stroke color? If have the following code
[[UINavigationBar appearance] setTitleTextAttributes:#{NSFontAttributeName:[UIFont fontWithName:#"Copperplate" size:19],
NSForegroundColorAttributeName:[UIColor whiteColor],
NSStrokeColorAttributeName:[UIColor colorWithRed:215/255.0f green:75/255.0f blue:40/255.0f alpha:1],
NSStrokeWidthAttributeName:#-3.0
}]; UI_APPEARANCE_SELECTOR;
The font name and color appear fine, but the stroke color and width are not reflected in the UI. Anyone figure this out?

How to change the colour of back button?

I have a map with just a back button at the top of the screen. For some reason it wont change colour for me. This is the code that I have in the delegate to change the other back button in the delegate.
UIBarButtonItem *barButtonAppearance = [UIBarButtonItem appearance];
[barButtonAppearance setTintColor:[UIColor blackColor]];
Cheers!
To change the text color of a single button you might set an attributed string as its title:
NSDictionary *attributes = #{NSForegroundColorAttributeName: [UIColor redColor]};
[button setTitleTextAttributes:attributes forState:UIControlStateNormal];
To change the color of all buttons in your navigation bar try:
self.navigationController.navigationBar.tintColor = [UIColor redColor];
The last one will also change the tint color if you have an image instead of text.
Both methods are tested unter iOS7.

Setting Blue Text Color of BarButtonItem in UINavigation bar fails in iOS 7

I'm trying to change the Navbar tint at runtime, and I'm able to change the navigation bar tint, but then the left and right buttons (which were system blue on iOS 7) changed, so I'm trying to force them back to the system blue color. this is the code I am using, called from viewDidLoad: I've tried both NSForegroundColorAttributeName and UITextAttributeTextColor, but I keep getting white or black text, depending on the color of the navigation bar.
How can I get the buttons to show blue text?
NSDictionary *btnAttr = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont boldSystemFontOfSize:14], UITextAttributeFont,
[UIColor blueColor], NSForegroundColorAttributeName,
nil];
for(UIView *v in [theView.view allSubViews])
{
if([v isKindOfClass:[UINavigationItem class]])
{
UINavigationItem *btn = (UINavigationItem *)v;
[btn.leftBarButtonItem setTitleTextAttributes:btnAttr forState:UIControlStateNormal];
[btn.rightBarButtonItem setTitleTextAttributes:btnAttr forState:UIControlStateNormal];
}
}
In iOS7 you if you need to change the navigationBar buttons color, you need to set tintColor:
navigationController.navigationBar.tintColor = [UIColor blueColor];
For better understanding check the image below:

Customize the NavigationBar backBarButton

I use the code below to customize the backBarButtonItem, because I need to change the text color.
[[UIBarButtonItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor blackColor], UITextAttributeTextColor, nil] forState:UIControlStateNormal];
The color changed but the text font looks weird. See, the "Back" is not clear around the edges.
I don't know why this happens. Does anyone has any idea about this?
The text has a shadow, which is set upward from the text. textlabel's have a shadowOffset value and a shadowColor value. You can either offset the shadow differently to make it look better or change the color to [UIColor clearColor] - both of which could help.
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
[attributes setValue:[UIColor blackColor] forKey:UITextAttributeTextColor];
[attributes setValue:[UIColor clearColor] forKey:UITextAttributeTextShadowColor];
[[UIBarButtonItem appearance] setTitleTextAttributes:attributes forState:UIControlStateNormal];
Replacing your code with the above snippet will set the text color to black, and the shadow to clear. This should remove the bluriness. The blur is actually caused by a shadow being cast by the text. By making the shadow a 'clearColor' it becomes invisible. An alternative would be changing the offset so that it looks like it's being cast downwards instead of up; or change the color to something distinctly different from black so that you can distinguish between the text and it's shadow.

Set UIBarButtonItem tint color always display default color

I have an IBAction responding to a selector that I want to use to change the displayed color of the icons. I set in Interface Builder a darkGrayColor for tint, but when changing the tintColor through code, the tint always goes to default color, making the icon white.
UIColor *dayColor = [UIColor darkGrayColor];
UIColor *nightColor = [UIColor lightGrayColor];
dayMode = !dayModeSwitch.on;
if (dayMode) {
[tocButton setTintColor:dayColor];
[actionButton setTintColor:dayColor];
[searchButton setTintColor:dayColor];
} else {
[tocButton setTintColor:nightColor];
[actionButton setTintColor:nightColor];
[searchButton setTintColor:nightColor];
}

Resources