14

Tôi có một số chế độ xem hiển thị trong bộ điều khiển điều hướng. Hai trong số các chế độ xem này có tiêu đề dài hơn cho thanh điều hướng.Cách thay đổi kích cỡ Tiêu đề trong thanh điều hướng động

Vấn đề là khi tiêu đề quá dài để vừa, một số ký tự bị cắt bớt và "..." được thêm vào.

Có cách nào tôi có thể yêu cầu thanh Điều hướng tự động định kích thước văn bản tiêu đề để vừa không?

+0

Kiểm tra liên kết này http://stackoverflow.com/questions/2422383/uinavigationbar-multi-line-title Nó sẽ giải quyết vấn đề của bạn hy vọng. – gopal

Trả lời

30

sử dụng mã dưới đây vào viewDidLoad.

Objective C

self.title = @"Your TiTle Text"; 
UILabel* tlabel=[[UILabel alloc] initWithFrame:CGRectMake(0,0, 200, 40)]; 
tlabel.text=self.navigationItem.title; 
tlabel.textColor=[UIColor whiteColor]; 
tlabel.font = [UIFont fontWithName:@"Helvetica-Bold" size: 30.0]; 
tlabel.backgroundColor =[UIColor clearColor]; 
tlabel.adjustsFontSizeToFitWidth=YES; 
tlabel.textAlignment = NSTextAlignmentCenter; 
self.navigationItem.titleView=tlabel; 

Swift Version

self.title = "Your TiTle Text" 
let tlabel = UILabel(frame: CGRectMake(0, 0, 200, 40)) 
tlabel.text = self.title 
tlabel.textColor = UIColor.whiteColor() 
tlabel.font = UIFont(name: "Helvetica-Bold", size: 30.0) 
tlabel.backgroundColor = UIColor.clearColor() 
tlabel.adjustsFontSizeToFitWidth = true 
tlabel.textAlignment = .center; 
self.navigationItem.titleView = tlabel 

Hy vọng nó hoạt động cho you.Thanks

+0

cảm ơn Bob đã làm việc rất đẹp! – PhoenixDev

+0

bạn được chào đón! – jamil

0

bạn cần phải cấu hình riêng danh hiệu thanh điều hướng với UILabel và cung cấp điều chỉnh kích thước font chữ ..

[self.navigationItem setTitleView:<"Include any UI View subclass">]; 
-1

Dưới đây là một ví dụ trong Swift cũng cho phép nhiều dòng. Sử dụng PureLayout để đơn giản hóa bố cục tự động.

override func viewDidLoad() { 
    super.viewDidLoad() 
    configureTitleView() 
} 

func configureTitleView() { 
    let titleLabel = UILabel() 
    titleLabel.numberOfLines = 0 
    titleLabel.textAlignment = .Center 
    titleLabel.font = UIFont.boldSystemFontOfSize(17.0) 
    titleLabel.text = searchLoc.mapItem.name 
    navigationItem.titleView = titleLabel 
    titleLabel.autoPinEdgesToSuperviewMargins() // PureLayout method 
    titleLabel.adjustsFontSizeToFitWidth = true 
} 

Và một ví dụ sử dụng:

enter image description here

+0

không có đường may để làm việc vì khi titleLabel.autoPinEdgesToSuperviewMargins được gọi, giám sát của titleLabel là (vẫn) nil và do đó thiết lập các ràng buộc xác nhận. (xcode 7.3, IOS9) – HixField

1

Không ai trong số các giải pháp đường may trên để làm việc đáng tin cậy đối với tôi. Tuy nhiên tôi đã tìm thấy một giải pháp bằng cách sử dụng các yếu tố khác nhau để cung cấp câu trả lời, trong Swift 2 và thực sự thanh lịch vì không yêu cầu bất kỳ mã tùy chỉnh nào mỗi khi bạn thay đổi nhãn, nó chỉ sử dụng các nhà quan sát thuộc tính trên tiêu đề. Lưu ý rằng trong trường hợp của tôi, tôi đã có một nút quay lại ở phía bên trái của thanh điều hướng, mà putted văn bản ra khỏi trung tâm của màn hình, để sửa lỗi này tôi đang sử dụng văn bản được phân bổ và tailIndent. Tất cả các ý kiến ​​/ thông tin trong đoạn code dưới đây:

class VCHowToTopic : UIViewController { 


    //add handlers so that any manipulation of the title is caught and transferred to the custom drawn UILabel 
    override var title : String? { 
     set { 
      super.title = newValue 
      configureTitleView() 
     } 
     get { 
      return super.title 
     } 
    } 

    //MARK: - lifecycle 


    func configureTitleView() { 
     //some large number that makes the navigationbar schrink down our view when added 
     let someVeryLargeNumber = CGFloat(4096) 
     //create our label 
     let titleLabel = UILabel(frame: CGRect(x: 0, y: 0, width: someVeryLargeNumber, height: someVeryLargeNumber)) 
     //0 means unlimited number of lines 
     titleLabel.numberOfLines = 0 
     //define style of the text (we will be using attributed text) 
     let style = NSMutableParagraphStyle() 
     style.alignment = .Center 
     //top compensate for the backbutton which moves the centered text to the right side of the screen 
     //we introduce a negative tail indent, the number of 56 has been experimentally defined and might 
     //depend on the size of your custom back button (if you have one), mine is 22x22 px 
     style.tailIndent = -56 
     //create attributed text also with the right color 
     let attrText = NSAttributedString(string: title!, attributes: [NSParagraphStyleAttributeName : style, 
      NSForegroundColorAttributeName : UIColor.whiteColor()]) 
     //configure the label to use the attributed text 
     titleLabel.attributedText = attrText 
     //add it as the titleview 
     navigationItem.titleView = titleLabel 
    } 


} 
+0

'CGRect (x: 0, y: 0, width: someVeryLargeNumber, height: someVeryLargeNumber)' là chìa khóa để hiển thị khung nhìn trong tiêu đề –

5

phiên bản Swift của Accepted trả lời + đặt văn bản nhãn trên trung tâm:

Swift 2.3:

self.title = "Your TiTle Text" 
    let tlabel = UILabel(frame: CGRectMake(0, 0, 200, 40)) 
    tlabel.text = self.title 
    tlabel.textColor = UIColor.whiteColor() 
    tlabel.font = UIFont.boldSystemFontOfSize(17) //UIFont(name: "Helvetica", size: 17.0) 
    tlabel.backgroundColor = UIColor.clearColor() 
    tlabel.adjustsFontSizeToFitWidth = true 
    tlabel.textAlignment = .Center 
    self.navigationItem.titleView = tlabel 

Và Swift 3:

self.title = "Your TiTle Text" 
    let frame = CGRect(x: 0, y: 0, width: 200, height: 40) 
    let tlabel = UILabel(frame: frame) 
    tlabel.text = self.title 
    tlabel.textColor = UIColor.white 
    tlabel.font = UIFont.boldSystemFont(ofSize: 17) //UIFont(name: "Helvetica", size: 17.0) 
    tlabel.backgroundColor = UIColor.clear 
    tlabel.adjustsFontSizeToFitWidth = true 
    tlabel.textAlignment = .center 
    self.navigationItem.titleView = tlabel 
0

Trong trường hợp bạn đã thêm chế độ xem vào titleView và bạn muốn đổi kích thước chế độ xem, bạn có thể sử dụng mã này (Swift 3):

self.translatesAutoresizingMaskIntoConstraints = false 
self.layoutIfNeeded() 
self.sizeToFit() 
self.translatesAutoresizingMaskIntoConstraints = true 
Các vấn đề liên quan