2013-01-24 38 views
5

Có thể lấy văn bản tiêu đề thu nhỏ để vừa với UINavigationBar trong iOS hay không.UINavigationBar văn bản nhãn tiêu đề

(đối với ứng dụng iPhone chân dung không có tự động trả lời).

Tôi đang đặt thanh tiêu đề động nhưng đôi khi văn bản quá dài và tại thời điểm nó chỉ cắt nó bằng dấu ba chấm.

tức là "Đây là t ..."

Tôi muốn thu nhỏ văn bản thay thế.

Trả lời

8

Bạn có thể tạo chế độ xem tiêu đề của riêng mình để thực hiện.

Something như thế này:

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

    //.... 
} 

Hope this helps.

+0

Cảm ơn, tôi đã thực sự làm điều gì đó tương tự như vậy. Đã sử dụng danh mục trên UILabel để tạo danh mục cho tôi một cách nhanh chóng trong tất cả các trình điều khiển chế độ xem. – 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 
Các vấn đề liên quan