Autoshrink Navigation Bar Title iOS 7 or Later - ios

I was wondering what is the best approach to make the UINavigationBar title auto shrink into minimum font size when it is too long?
Below are my example of the problem
Normal
Too Long (need to shrink the label)
And here is the codes I am using to do styling for UINavigationBar
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageWithColor:[UIColor greenColor]]
forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
[[UINavigationBar appearance] setTitleTextAttributes:#{NSForegroundColorAttributeName : [UIColor whiteColor],
NSFontAttributeName : [fontWithName:#"Helvetica-Bold" size:18.0]}];
[[UINavigationBar appearance] setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleRightMargin];

The most common solution is to use a custom view for the title. But you can't do this using UIAppearance, you will need to do this on each UIViewController that you push, or make a category of the UINavigationBar, and add this logic every time the title text is changed (Not sure if this will work or if it's a good idea, though) or create a new adhoc method.
For example (Put this in the viewDidLoad of your UIViewController):
UILabel* titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0, 200, 40)];
titleLabel.text = #"Your very long title";
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.textColor = [UIColor whiteColor];
titleLabel.adjustsFontSizeToFitWidth = YES; // As alternative you can also make it multi-line.
titleLabel.minimumScaleFactor = 0.5;
self.navigationItem.titleView = titleLabel;

Related

UINavigationBar back button item font color won't set

I have a navigation bar on a viewController that I can enable/disable. The problem is I can't get the font for the UIBarButtonItems to change colors after the view initially loads, though the back arrow will change.
I disable the UINavigationBar on myViewController with the following line of code:
self.navigationController.navigationBar.userInteractionEnabled = NO;
I have UIControlStates configured in AppDelegate.m for UIBarButtonItems for enabled and disabled, but nothing happens to the font after the view initially loads.
In my AppDelegate.m, I have the following code to set the UINavigationBar's font and color:
// UIBarButtonItem styling
NSShadow *shadow = [[NSShadow alloc]init];
shadow.shadowOffset = CGSizeMake(0.0, 1.0);
shadow.shadowColor = [UIColor whiteColor];
// enabled
NSDictionary *enabledTextAttributeDictionary = #{NSForegroundColorAttributeName : [UIColor customCellHyperlinkColor], NSShadowAttributeName: shadow, NSFontAttributeName:[UIFont fontWithName:#"GillSans" size:17.0]};
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTitleTextAttributes:enabledTextAttributeDictionary forState:UIControlStateNormal];
// disabled
NSDictionary *disabledTextAttributeDictionary = #{NSForegroundColorAttributeName : [UIColor lightGrayColor], NSShadowAttributeName: shadow, NSFontAttributeName:[UIFont fontWithName:#"GillSans" size:17.0]};
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTitleTextAttributes:disabledTextAttributeDictionary forState:UIControlStateDisabled];
// UINavigationBarTitle styling
NSDictionary *titleAttributeDictionary = #{NSForegroundColorAttributeName : [UIColor blackColor], NSShadowAttributeName: shadow, NSFontAttributeName:[UIFont fontWithName:#"GillSans" size:19.0]};
[[UINavigationBar appearanceWhenContainedIn:[UINavigationController class], nil]setTitleTextAttributes:titleAttributeDictionary];
I thought since I configured the 2 states (UIControlStateNormal/UIControlStateDisabled) in AppDelegate.m, nothing needs to be done other than enable/disable the navigationBar. Enable/disable does indeed enable/disable, but the color on the back button label doesn't change (though it does initially set to the color I set for UIControlStateNormal in AppDelegate).
I tried to manually set it, but the label on the backButtonItem stays blue while the icon to the left of it tints light gray. What (obvious thing) am I missing that is preventing me from changing the color of my back button font?
I think you are looking for this
Add this code in you AppDelegate.m
NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowOffset = CGSizeMake(0.0, 1.0);
shadow.shadowColor = [UIColor clearColor];
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil]
setTitleTextAttributes:
#{NSForegroundColorAttributeName:[UIColor redColor],
NSShadowAttributeName:shadow,
NSFontAttributeName:[UIFont systemFontOfSize:13.0]
}
forState:UIControlStateNormal];
I think you need to use
[self.navigationController.navigationBar setBarTintColor:[UIColor lightGrayColor]];
self.navigationController.navigationBar.tintColor = [UIColor lightGrayColor];
[self.navigationController.navigationBar setTitleTextAttributes:#{NSForegroundColorAttributeName : [UIColor lightGrayColor], NSFontAttributeName : place your font here}];
instead of
self.navigationController.navigationBar.tintColor = [UIColor lightGrayColor];

iOS 7 UINavigationBar appearance not working first timeā€¦

I am trying to change the look of the UINavigationBar in my iOS7 app. I am doing the following:
- (void)viewDidLoad
{
[super viewDidLoad];
m_sNumberToCall = #"";
UIBarButtonItem * btn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"IconHome.png"] style:UIBarButtonItemStyleBordered target:self action:#selector(btHomeTouched:)];
self.navigationItem.leftBarButtonItem = btn;
self.navigationController.navigationBar.translucent = YES;
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"TVCNavBack.png"] forBarMetrics:UIBarMetricsDefault];
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:#"Helvetica-Bold" size:21.0],
NSFontAttributeName,
nil]];
}
But, the first time I present the UITableViewController it is the standard iOS7 nav bar, then I press home and present it again and it is my new look.
Any ideas why it does not work the first time?
Don't change the appearance but the navigation bar directly. The appearance affects only the future instances but not the already created ones.
Change:
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"TVCNavBack.png"] forBarMetrics:UIBarMetricsDefault];
to:
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"TVCNavBack.png"] forBarMetrics:UIBarMetricsDefault];
The answer before only helps you with the background image but not with the title text attributes.
You don't need to change your code but all you have to do is move it to
applicationDidFinishLaunchingWithOptions
in your AppDelegate.m file.

uinavigationbar background image hides title

When i add background image to uinavigationbar it hides my title on uinavigationbar,
Here is my code,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
...
...
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed: #"BarBg.png"] forBarMetrics:UIBarMetricsDefault];
}
Code in Class:
int height = self.navigationController.navigationBar.frame.size.height;
int width = self.navigationController.navigationBar.frame.size.width;
UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, height)];
navLabel.text=#"Categories";
navLabel.backgroundColor = [UIColor clearColor];
navLabel.textColor = [UIColor whiteColor];
navLabel.font = [UIFont boldSystemFontOfSize:17];
navLabel.textAlignment = NSTextAlignmentCenter;
self.navigationItem.titleView = navLabel;
After writing this code i am unable to see title on navigation bar, title is behind navigationBar image.
Please help, Thanks in advance..
instead of set label as a titleView of UINavigationBar add that UILabel as a Subview of NavigationBar like bellow...
[self.navigationController.navigationBar addSubview:navLabel];
[self.navigationController.navigationBar bringSubviewToFront:navLabel];
If you're looking only to customize your navigationBar title, I recommend using the appearance instead of adding a label to your navbar.
[[UINavigationBar appearance] setTitleTextAttributes:#{
UITextAttributeFont: [UIFont fontWithName:yourFontName size:fontSize],
UITextAttributeTextColor :[UIColor whiteColor],
}];

Custom navigation bar minimum font scale?

I need to set a minimum font scale on my navigation bar. I set title attributes in my AppDelegate.m:
[[UINavigationBar appearance] setTitleTextAttributes: #{
UITextAttributeTextColor: [UIColor colorWithRed:54/255.0f green:54/255.0f blue:54/255.0f alpha:1.0f],
UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetMake(0.0f, 0.0f)],
UITextAttributeFont: [UIFont fontWithName:#"HelveticaNeue-Bold" size:20.0f]
}];
My navigation bar is dragged and dropped on a UIView in my storyboard. Everything works fine with cusom navigation bar image and the text attributes, except I need to set the minimum font scale if it's possible.
I solved it by creating a label and use as navigation bar title.
// NavBar title
UILabel *customNavBarTitle = [[UILabel alloc] initWithFrame:CGRectMake(44, 0, 320, 44)];
customNavBarTitle.textAlignment = NSTextAlignmentCenter;
customNavBarTitle.adjustsFontSizeToFitWidth = YES;
[customNavBarTitle setFont:[UIFont fontWithName:#"OpenSans-Semibold" size:15.0f]];
[customNavBarTitle setBackgroundColor:[UIColor clearColor]];
[customNavBarTitle setTextColor:[UIColor blackColor];
[customNavBarTitle setText:customTitle];
[self.view addSubview:customNavBarTitle];

changing the MFMailComposeViewController navigationBar title font

I have a MFMailComposeViewController and when I've set the subject it also sets it as the subject.
The question is how do I customize the font of the title and color?
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
[picker setSubject:#"Feedback for MyApp"];
i've changed the font generally for my apps from the app-delegate for all navigation bars of all view controllers. if you don't mind it changing all of your navigation bar title fonts, put something like the following in applicationDidFinishLaunching:withOptions:
if ([[UINavigationBar class] respondsToSelector:#selector(appearance)])
// set the navigation bar font to "courier-new bold 14"
[[UINavigationBar appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:[UIColor lightGrayColor], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(1, 0)], UITextAttributeTextShadowOffset,
[UIFont fontWithName:#"CourierNewPS-BoldMT" size:14.0], UITextAttributeFont,
nil]];
update:
if you want it specific to this one case, you can probably use appearanceWhenContainedIn: using the arguments Alexander Akers suggested in the comment below, since UIViewController inherits from UIAppearanceContainer. i know appearanceWhenContainedIn: works in other cases such as UIBarButtonItems contained in UINavigationBar, so it should work similarly for this situation.
My guess is it can't be done because of cross-process restrictions. I can change the color, size, and font (if it's a built-in system font), but attempts to change it to any of the fonts included in my app fail.
Since the MFMailComposeViewController is an instance of a UINavigationController, you can set the titleView of its topViewController as follows:
UIViewController *controller = [mailController topViewController];
UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 170.0f, 44.0f)];
titleView.backgroundColor = [UIColor clearColor];
UILabel *label = [[UILabel alloc] initWithFrame:titleView.bounds];
label.textColor = [UIColor whiteColor];
label.backgroundColor = [UIColor clearColor];
label.text = #"Custom Font";
label.font = [UIFont italicSystemFontOfSize:27.0f];
label.textAlignment = UITextAlignmentCenter;
[titleView addSubview:label];
[label release];
controller.navigationItem.titleView = titleView;
[titleView release];

Resources