I am using this for changing the text color of text of segmented control but its color is not getting changed in UIControlStateNormal. I have to kept the same color for both states.
NSDictionary *highlightedAttributes = [NSDictionary
dictionaryWithObject:[UIColor blueColor] forKey:UITextAttributeTextColor];
[self.segmentController
setTitleTextAttributes:highlightedAttributes forState:UIControlStateSelected];
You have to set any attributes explicitly for the normal state (UIControlStateNormal) as well.
Try this:
NSDictionary *textAttributes = #{NSFontAttributeName: [UIFont systemFontOfSize:13],
UITextAttributeTextColor: UIColorFromRGB(0x93938D),
UITextAttributeTextShadowColor: [UIColor clearColor]
};
[self.segmentController setTitleTextAttributes:textAttributes forState:UIControlStateNormal];
[self.segmentController setTitleTextAttributes:textAttributes forState:UIControlStateSelected];
Related
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];
I am setting appearance of UIBarButtonItem using -appearance method.
How can I change it back to iOS default appearance?
Here I am changing appearance of UIBarButtonItem:
NSDictionary *textAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor colorWithHex:0x3A4047],
UITextAttributeTextColor,
[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.0],
UITextAttributeTextShadowColor,
[UIFont fontWithName:#"Helvetica-Bold" size:13.0],
UITextAttributeFont,
nil];
[[UIBarButtonItem appearance] setTitleTextAttributes:textAttributes
forState:UIControlStateNormal];
Now at some point can I revert all that appearance proxies that I have applied to default proxies?
[[UIBarButtonItem appearance] setTitleTextAttributes:nil
forState:UIControlStateNormal];
Create an NSDictionary with all of the default attributes before you create *textAttributes. Then when you want to make it default set the attributes to the other NSDictionary.
I have a UISegmentedControl that I'm trying to customize, and I'm having two problems :
This is my code :
NSDictionary *attributes = #{UITextAttributeFont: [UIFont fontWithName:#"Gotham-Book" size:14.f],
UITextAttributeTextColor: [UIColor whiteColor],
UITextAttributeTextShadowColor: [UIColor clearColor]};
[segmentedControl setTitleTextAttributes:attributes forState:UIControlStateNormal];
NSDictionary *selectedAttributes = #{UITextAttributeFont: [UIFont fontWithName:#"Gotham-Medium" size:14.f],
UITextAttributeTextColor: [UIColor redColor]};
[segmentedControl setTitleTextAttributes:selectedAttributes forState:UIControlStateSelected];
1) The UISegmentedControl reacts well for the UIControlStateNormal but for the forState:UIControlStateSelected, the color works but not the font. FYI, the font exists because if I switch "Gotham-Book" and "Gotham-Medium" for UIControlStateNormal, I see the difference.
2) Before I've set the font for the UIControlStateNormal, the text was vertically aligned, but now it's not anymore. How could I change the textInsets ?
Thank's for your help !
On IOS 7 use NSFontAttributeName, NSForegroundColorAttributeName and NSShadowAttributeName (if needed),
For backward compatibility I using both variants, like:
NSDictionary *attributes = #{UITextAttributeFont: [UIFont fontWithName:#"Gotham-Book" size:14.f],
UITextAttributeTextColor: [UIColor whiteColor],
UITextAttributeTextShadowColor: [UIColor clearColor],
NSFontAttributeName: [UIFont fontWithName:#"Gotham-Book" size:14.f],
NSForegroundColorAttributeName: [UIColor whiteColor],
};
[segmentedControl setTitleTextAttributes:attributes forState:UIControlStateNormal];
UPDATE ANSWERED. BY ME.
Currently having problems with the text color change of my UISegmentedControl; it needs to change on first load with UIControlStateSelected. Code works, but only conditionally. It works when you visit the page with the segmented control on the navigation bar, hit the back button, and then visit the page again. I'm assuming there's a problem with inheritance here. Let me explain..
The location of the the segmented control lies on top of my navigation bar.
Inheritance of the ViewController which contains the SegmentedControl:
TabBarViewController(managed with AppDelegate)-->navigation Controller-->ViewController(where 'inviteSegBar' lies)
Here's the code within AppDelegate.m:
[[UINavigationBar appearance] setBarTintColor:[UIColor colorWithHexString:#"#669900"]];//this one sets it green.
[[UINavigationBar appearance] setBackgroundColor:[UIColor whiteColor]];
And here's the viewDidLoad: code for the VC which contains 'inviteSegBar', the UISegmentedControl in question:
- (void)viewDidLoad
{
[super viewDidLoad];
//CUSTOM APPEARANCE <below>
self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
self.navigationController.navigationBar.tintColor = [UIColor colorWithHexString:#"#669900"];
inviteSegBar.tintColor = [UIColor colorWithHexString:#"#333333"];
[[UISegmentedControl appearance] setTitleTextAttributes:#{NSForegroundColorAttributeName : [UIColor colorWithHexString:#"#669900"]} forState:UIControlStateSelected];
}
Like I said the last line works, but only when you re-visit the page. Why is this happening?
PS This is the same issue guys, I had already tried this code before any of the answers were listed.
ANSWER FOUND: Simply move
[[UISegmentedControl appearance] setTitleTextAttributes:#{NSForegroundColorAttributeName : [UIColor colorWithHexString:#"#669900"]} forState:UIControlStateSelected];
to your AppDelegate.m file
Use
UIColor *whitecolor = [UIColor whiteColor];
NSDictionary *attributes = [NSDictionary dictionaryWithObjects:#[whitecolor] forKeys:#[UITextAttributeTextColor]];
[yourSegment setTitleTextAttributes:attributes
forState:UIControlStateNormal];
UIColor *grayColor = [UIColor darkGrayColor];
NSDictionary *attributes = [NSDictionary dictionaryWithObjects:#[grayColor] forKeys:#[UITextAttributeTextColor]];
[yourSegment setTitleTextAttributes:attributes
forState:UIControlStateSelected];
update
UIColor *whitecolor = [UIColor whiteColor];
NSDictionary *attributes = [NSDictionary dictionaryWithObjects:#[whitecolor] forKeys:#[NSForegroundColorAttributeName]];
[yourSegment setTitleTextAttributes:attributes
forState:UIControlStateNormal];
UIColor *grayColor = [UIColor darkGrayColor];
NSDictionary *attributes = [NSDictionary dictionaryWithObjects:#[grayColor] forKeys:#[NSForegroundColorAttributeName]];
[yourSegment setTitleTextAttributes:attributes
forState:UIControlStateSelected];
This code allows you to set some text attributes for label in segmented control:
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor blackColor], UITextAttributeTextColor,
nil];
[_segmentedControl setTitleTextAttributes:attributes forState:UIControlStateSelected];
More allowed attributes in Apple documentation: link
This may help you:
UIAppearance proxy to set title text attributes but preserve tintColor for borders.
[[UISegmentedControl appearance] setTitleTextAttributes:#{
NSForegroundColorAttributeName : [UIColor redColor]
} forState:UIControlStateNormal];
For change UISegmentedControl appearance insert for example into viewDidLoad function this code:
// color selected text ---> red
[[UISegmentedControl appearance] setTitleTextAttributes:#{ NSForegroundColorAttributeName : [UIColor redColor] } forState:UIControlStateSelected];
// color disabled text ---> blue
[[UISegmentedControl appearance] setTitleTextAttributes:#{ NSForegroundColorAttributeName : [UIColor blueColor] } forState:UIControlStateNormal];
// color tint segmented control ---> black
[[UISegmentedControl appearance] setTintColor:[UIColor greenColor]];
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];