UINavigationbar title is cut off when using titleTextAttributes
Asked Answered
R

3

16

My iPhone app has a tab bar controller and on each tab (4 tabs) there is a UINavigationController that I want to be a different color and change the font on the title, so I made my own custom navigation controller to use everywhere so those changes are only in 1 place. The tint is easy and works fine, but when I try to set the font using setTitleTextAttributes, it changes the font but on some views the title shows up being cut off at the end (ie. "My titl..."). If I change views and then come back to the view with the cut off title, the title does show up properly though.

Currently my viewDidLoad in my custom UINavigationController has:

UIFont *font = [UIFont fontWithName:@"GillSans-Bold" size:22];
NSDictionary *attr = [[NSDictionary alloc] initWithObjectsAndKeys:font, UITextAttributeFont, nil];
[self.navigationBar setTitleTextAttributes:attr];

My thought was that it's changing the font of the titleView but it's not being resized in accordance to the new size (since it's a bigger font). Another issue is when the phone is turned to landscape, any letters that hang low (g, p, y) have the bottoms cut off. Is there a way to resize the label or allow for minimumFontSize to be set so the text simply shrinks when it's too big?

Radically answered 25/12, 2011 at 5:56 Comment(0)
K
3

This worked for me

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self setTitle:self.station.title];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 400, 44)];
    label.backgroundColor = [UIColor clearColor];
    [label setFont:[UIFont fontWithName:@"GillSans-Bold" size:24.0]];
    label.textAlignment = UITextAlignmentCenter;
    label.textColor = [UIColor whiteColor];
    label.adjustsFontSizeToFitWidth = YES;
    label.text = self.title;    
    self.navigationItem.titleView = label;
}
Karakoram answered 4/5, 2012 at 10:44 Comment(1)
@JavierSoto rather?Hollander
R
0

I think this problem is w.r.t rootviewcontroller. The tab bar is defined in your root view controller right?

From there if you push and pop via navigation controllers - all the contents will be the same irrespective of the navigation.

Rah answered 20/9, 2012 at 16:39 Comment(0)
S
0

This worked for me:

+ (void)setNavBarTitle:(NSString *)title forUINavigationItem:(UINavigationItem*)navigationItem
{
    UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 0)] autorelease];
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont boldSystemFontOfSize:24];
    label.textAlignment = UITextAlignmentCenter;
    label.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
    label.textColor = [UIColor whiteColor];
    label.adjustsFontSizeToFitWidth = YES;
    label.text = title;
    [label sizeToFit];
    navigationItem.titleView = label;
}
Schism answered 17/1, 2013 at 9:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.