Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting font and font-size of title bar text in a UITableViewController

I have a simple navigation based application for the iphone/objective-c

within various UIViewControllers that are pushed into view, I can set the text in the title bar using something like

self.title = @"blah blah blah"

Is there a way to control the font and font-size of the title in the title bar text?

thanks!

like image 895
user141146 Avatar asked Oct 09 '09 20:10

user141146


2 Answers

the proper way to resize the title text of a navcontroller is to set the titleView property of the navigationItem

like this (in viewDidLoad)

   UILabel* tlabel=[[UILabel alloc] initWithFrame:CGRectMake(0,0, 300, 40)];
        tlabel.text=self.navigationItem.title;
    tlabel.textColor=[UIColor whiteColor];
    tlabel.backgroundColor =[UIColor clearColor];
    tlabel.adjustsFontSizeToFitWidth=YES;
    self.navigationItem.titleView=tlabel;
like image 132
roocell Avatar answered Oct 13 '22 21:10

roocell


You may want to emboss the label so it doesn't look fuzzy and flat:

- (void)viewDidLoad
{
    [super viewDidLoad];

    CGRect frame = CGRectMake(0, 0, 400, 44);
    UILabel *label = [[[UILabel alloc] initWithFrame:frame] autorelease];
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont boldSystemFontOfSize:18.0];
    label.textAlignment = UITextAlignmentCenter;
    label.textColor = [UIColor whiteColor];
    label.text = self.navigationItem.title;
    // emboss so that the label looks OK
    [label setShadowColor:[UIColor darkGrayColor]];
    [label setShadowOffset:CGSizeMake(0, -0.5)];
    self.navigationItem.titleView = label;
}
like image 24
Jonathan Moffatt Avatar answered Oct 13 '22 19:10

Jonathan Moffatt