1

I have a navigation bar title that gets truncated if too long - based on the following code, how could the issue be fixed so that the title is displayed on 2 lines at runtime?

    override func viewDidLoad() {
        super.viewDidLoad()

    title = checklist.name

    self.navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "CollegiateHeavyOutline", size: 23.0)!,
        NSForegroundColorAttributeName: UIColor.init(red: 25.0/255.0, green: 25.0/255.0, blue: 112.0/255.0, alpha: 1.0)]

}

The screenshot below show the title with text size 17 (uses 2 lines - nice!) enter image description here

But the following is not so nice and the title should read 'But this one is cut off with size 18 and over enter image description here

Any thoughts?

Laroms
  • 85
  • 1
  • 13

1 Answers1

2

Is that what are you looking for?

override func viewDidLoad() {
    super.viewDidLoad()

    let titleLabel = UILabel()
    titleLabel.backgroundColor = UIColor.clearColor()
    titleLabel.numberOfLines = 2
    titleLabel.font = UIFont(name: "CollegiateHeavyOutline", size: 23.0)
    titleLabel.textColor = UIColor(red: 25.0/255.0, green: 25.0/255.0, blue: 112.0/255.0, alpha: 1.0)
    titleLabel.textAlignment = .Center
    titleLabel.text = checklist.name
    titleLabel.sizeToFit()
    navigationItem.titleView = titleLabel
}
iyuna
  • 1,787
  • 20
  • 24