0

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?

David
  • 2,898
  • 3
  • 21
  • 57
  • The navigation bar lets you insert `UILabel`, which means that you can automatically adjust the font size, I suppose. – El Tomato Jun 26 '20 at 06:20
  • Consider my answer on this question: https://stackoverflow.com/questions/14956561/how-to-resize-title-in-a-navigation-bar-dynamically/62043519#62043519 To work with normal titles you will have to set the font to ```UIFont.systemFont(ofSize: 17, weight: .semibold)``` and ```largeTitleTextAttributes``` to ```titleTextAttributes``` – finebel Jun 26 '20 at 08:52
  • And you will have to adjust this line ```tempLargeTitleLabel.intrinsicContentSize.width > view.frame.width - 30``` (instead of 30 I use for normal titles 195). Inside the for-loop and in the end where you actually set your ```titleTextAttributes``` you have to set the font-weight to ```.semibold``` too. – finebel Jun 26 '20 at 08:59

1 Answers1

4

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