简体   繁体   中英

Automatically adjust Navigation Item Title font size to fit

I'm using the newest version of Xcode and Swift .

I have a Navigation Bar with a Navigation Item Title . I'm using a normal small title, not the iOS 13 large title.

On some screens the Navigation Item Title is too long, so it gets truncated with a trailing

Is there a way to auto adjust the font size to fit the title in Navigation Item Title ?

Place the code below in viewDidLoad(), this should scale down the size of the text in your title without truncating it.:

    let navbarTitle = UILabel()
    navbarTitle.text = "My nav title"
    navbarTitle.minimumScaleFactor = 0.5
    navbarTitle.adjustsFontSizeToFitWidth = true
    navigationItem.titleView = navbarTitle

Hopefully this helps you out. Play with the minimumScaleFactor to find a value you'd like to have. If you need the title to be bold: How to make a UILabel bold .

Additionally here's a link that might be of use to you: Line break mode and number of lines on UILabel

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM