在UITableViewController中设置标题栏文本的字体和字体大小

时间:2009-10-09 20:47:32

标签: iphone objective-c

我有一个基于导航的简单应用程序,适用于iphone / objective-c

在推入视图的各种UIViewControllers中,我可以使用类似

之类的东西在标题栏中设置文本
self.title = @"blah blah blah"

有没有办法控制标题栏文字中标题的字体字体大小

谢谢!

4 个答案:

答案 0 :(得分:17)

调整navcontroller标题文本大小的正确方法是设置navigationItem的titleView属性

像这样(在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;

答案 1 :(得分:4)

你可能想要对标签进行压花,使其看起来不模糊和平坦:

- (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;
}

答案 2 :(得分:3)

您可以将任何UIView分配给navcontroller的标题区域。

创建一个UILabel并根据需要设置其字体和大小,然后将其分配给UIViewController的navigationItem.titleView属性。确保UILabel的backgroundColor设置为clearColor。

这仅适用于顶级导航视图。当用户向下钻入视图控制器层次结构并显示“后退”按钮时,将忽略备用titleView并显示常规文本标签。

答案 3 :(得分:3)

如果您希望这在iphone和ipad上都能正常工作,并希望将标题置于中心位置,请使用以下代码。

 - (void)viewDidLoad
{
    [super viewDidLoad];

    UILabel* label=[[UILabel alloc] initWithFrame:CGRectMake(0,0, self.navigationItem.titleView.frame.size.width, 40)];
    label.text=self.navigationItem.title;
    label.textColor=[UIColor whiteColor];
    label.backgroundColor =[UIColor clearColor];
    label.adjustsFontSizeToFitWidth=YES;
    label.font = [AppHelper titleFont];
    label.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView=label;
}
相关问题