I am using navigation controller, and I\'ve set to true its navigation bar\'s prefersLargeTitle
property. Everything works fine, but when the text of my title becom
Made an edit to @vicente.fava answer - this works great.
self.title = longTitle
self.navigationController?.navigationBar.prefersLargeTitles = true
adjustLargeTitleSize()
extension UIViewController {
func adjustLargeTitleSize() {
guard let title = title, #available(iOS 11.0, *) else { return }
let maxWidth = UIScreen.main.bounds.size.width - 60
var fontSize = UIFont.preferredFont(forTextStyle: .largeTitle).pointSize
var width = title.size(withAttributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: fontSize)]).width
while width > maxWidth {
fontSize -= 1
width = title.size(withAttributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: fontSize)]).width
}
navigationController?.navigationBar.largeTitleTextAttributes =
[NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: fontSize)
]
}
}