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];
Related
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];
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;
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],
}];
I modified my navigation bar as follows in app delegate:
NSDictionary *settings = #{
UITextAttributeFont : [UIFont fontWithName:#"impact" size:36.0],
UITextAttributeTextColor : [UIColor whiteColor],
UITextAttributeTextShadowColor : [UIColor clearColor],
UITextAttributeTextShadowOffset : [NSValue valueWithUIOffset:UIOffsetZero]};
[[UINavigationBar appearance] setTitleTextAttributes:settings];
But the font cuts down like this for the larger font. :
I tried doing this in viewWillAppear of my VC, :
UIView *newTitleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[newTitleView setBackgroundColor:[UIColor blackColor]];
[self.navigationController.navigationBar addSubview:newTitleView];
Then I was going to align title to center. But that just doesn't seems right. Only thing I actually need is to remove margins at top and bottom of the title Label. How do I go about it.
Try this code and don't set setTitleTextAttributes
UILabel *titleView = (UILabel *)self.navigationItem.titleView;
if (!titleView) {
titleView = [[UILabel alloc] initWithFrame:CGRectZero];
titleView.backgroundColor = [UIColor clearColor];
titleView.font = [UIFont boldSystemFontOfSize:20.0];
titleView.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
titleView.textColor = [UIColor yellowColor]; // Change to desired color
self.navigationController.navigationItem.titleView = titleView;
[titleView release];
}
titleView.text = title;
[titleView sizeToFit];
Well the easiest way i would suggest is to use a UIView with label and button customised for the requirement and use it,not on navigation bar.
mnavigation bar height customization is the trouble that the height is fixed and the subview also it may affect .So I suggest to go with a custom header view subclassed with UIView
You can subclass to the UILabel and override drawTextInRect: like this:
- (void)drawTextInRect:(CGRect)rect {
UIEdgeInsets insets = {0, 5, 0, 5};
return [super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)];
}
For more useful link you can find
here
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];