Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UINavigationbar title is cut off when using titleTextAttributes

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?

like image 486
Stephen Avatar asked Dec 25 '11 05:12

Stephen


1 Answers

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;
}
like image 120
HazA Avatar answered Oct 23 '22 12:10

HazA