如何在iOS中格式化UINavigationItem的标题?

时间:2013-01-12 10:44:36

标签: ios cocoa-touch uinavigationitem

  

可能重复:
  UINavigationBar multi-line title

我正在尝试在iOS中设置UINavigationItem's标题的格式。

通常情况下,我们可以使用以下内容。

self.naviItem.title = @"Hello";

但是我想在TITLE中设置三行文字,如下图所示。

enter image description here

那我该怎么办?

感谢您的阅读。 :)

2 个答案:

答案 0 :(得分:2)

您必须使用 titleView 以这种方式自定义 NavigationItem

  UILabel* tlabel=[[UILabel alloc] initWithFrame:CGRectMake(0,0, 300, 40)];
        tlabel.text=self.title;
        tlabel.textAlignment=NSTextAlignmentCenter;
        tlabel.font = [UIFont boldSystemFontOfSize:17.0];
        tlabel.numberOfLines = 3
        tlabel.textColor=[UIColor colorWithRed:7.0/255.0 green:26.0/255.0 blue:66.0/255.0 alpha:1.0];
        tlabel.backgroundColor =[UIColor clearColor];
        tlabel.adjustsFontSizeToFitWidth=YES;
        self.navigationItem.titleView=tlabel;

答案 1 :(得分:1)

你需要做的是制作一个自定义视图(UIView的子类,在这种情况下,3个UILabel作为视图的子级)。 您可以将此自定义视图放在导航栏中,如下所示:

self.navigationItem.titleView = myCustomView;
祝你好运!