Skip to content

Instantly share code, notes, and snippets.

@zwang
Created October 20, 2016 01:00
Show Gist options
  • Save zwang/a9ddeda0f2e03aef9e8ac64b7eb41748 to your computer and use it in GitHub Desktop.
Save zwang/a9ddeda0f2e03aef9e8ac64b7eb41748 to your computer and use it in GitHub Desktop.
two lines title for navigationbar
//http://stackoverflow.com/questions/2422383/uinavigationbar-multi-line-title
private func setupTitleView() {
let topText = NSLocalizedString("key", comment: "")
let bottomText = NSLocalizedString("key", comment: "")
let titleParameters = [NSForegroundColorAttributeName : UIColor.<Color>(),
NSFontAttributeName : UIFont.<Font>]
let subtitleParameters = [NSForegroundColorAttributeName : UIColor.<Color>(),
NSFontAttributeName : UIFont.<Font>]
let title:NSMutableAttributedString = NSMutableAttributedString(string: topText, attributes: titleParameters)
let subtitle:NSAttributedString = NSAttributedString(string: bottomText, attributes: subtitleParameters)
title.appendAttributedString(NSAttributedString(string: "\n"))
title.appendAttributedString(subtitle)
let size = title.size()
let width = size.width
guard let height = navigationController?.navigationBar.frame.size.height else {return}
let titleLabel = UILabel(frame: CGRectMake(0,0, width, height))
titleLabel.attributedText = title
titleLabel.numberOfLines = 0
titleLabel.textAlignment = .Center
navigationItem.titleView = titleLabel
}
@rajajawahar
Copy link

Its works like a champ :-)

@akhalsa
Copy link

akhalsa commented Dec 2, 2018

super helpful!

@CharlesMich
Copy link

thank you for the code. I am still new to Xcode and swift. I do most of the work in the storyboard. My question is, where do I paste the code above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment