David
David

Reputation: 3348

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?

Upvotes: 0

Views: 2561

Answers (1)

Gayal Rupasinghe
Gayal Rupasinghe

Reputation: 144

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

Upvotes: 4

Related Questions