Vertically centering font in Segmented Controller - ios

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

Related

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

UISegmentedControl setTitleTextAttributes:forState: not working well with font

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

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

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?

Resources