2013-01-24 93 views
5

是否有可能让标题文字缩小以适应iOS中的UINavigationBarUINavigationBar标题标签文字

(对于没有自动布局的肖像iPhone应用程序)。

我正在动态设置标题栏,但有时文本太长,此刻它只是用省略号将其切断。

即“这是T ...”

我想它缩小文字。

回答

8

您可以创建自己的标题视图来制作它。

事情是这样的:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    //Do any additional setup after loading the view, typically from a nib. 
    UILabel *titleLabelView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 40)]; //<<---- Actually will be auto-resized according to frame of navigation bar; 
    [titleLabelView setBackgroundColor:[UIColor clearColor]]; 
    [titleLabelView setTextAlignment: NSTextAlignmentCenter]; 
    [titleLabelView setTextColor:[UIColor whiteColor]]; 
    [titleLabelView setFont:[UIFont systemFontOfSize: 27]]; //<<--- Greatest font size 
    [titleLabelView setAdjustsFontSizeToFitWidth:YES]; //<<---- Allow shrink 
    // [titleLabelView setAdjustsLetterSpacingToFitWidth:YES]; //<<-- Another option for iOS 6+ 
    titleLabelView.text = @"This is a Title"; 

    navigationBar.topItem.titleView = titleLabelView; 

    //.... 
} 

希望这有助于。

+0

谢谢,我实际上已经做了类似的事情了。在UILabel上使用一个类别在所有视图控制器中快速创建它。 – Fogmeister

2
Xcode 7.1 - Swift 2.0 


//Adding Title Label 
     var navigationTitlelabel = UILabel(frame: CGRectMake(0, 0, 200, 21)) 
     navigationTitlelabel.center = CGPointMake(160, 284) 
     navigationTitlelabel.textAlignment = NSTextAlignment.Center 
     navigationTitlelabel.textColor = UIColor.whiteColor() 
     navigationTitlelabel.text = "WORK ORDER" 
     self.navigationController!.navigationBar.topItem!.titleView = navigationTitlelabel