Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rajajawahar/fbbcdf7b58f008bc7cdd5c686bb02a29 to your computer and use it in GitHub Desktop.
Save rajajawahar/fbbcdf7b58f008bc7cdd5c686bb02a29 to your computer and use it in GitHub Desktop.

Revisions

  1. Zhao Wang revised this gist Oct 20, 2016. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions two lines title for navigationbar.swift
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    //http://stackoverflow.com/questions/2422383/uinavigationbar-multi-line-title
    private func setupTitleView() {
    let topText = NSLocalizedString("key", comment: "")
    let bottomText = NSLocalizedString("key", comment: "")
  2. Zhao Wang created this gist Oct 20, 2016.
    27 changes: 27 additions & 0 deletions two lines title for navigationbar.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    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
    }