0

I have used UINavigationBar.appearance() in swift. By using this, I have changed the backgroundColor and the textColor of UINavigationBarusing the below code.

However, I can't find the numberOfLinesattribute of the UINavigationBar title text. Any help on this is appreciated.

 var naviAppreance = UINavigationBar.appearance()
 naviAppreance.tintColor = uicolorFromHex(0xffffff)
 naviAppreance.barTintColor = uicolorFromHex(0x00cc66)
 naviAppreance.titleTextAttributes = [ NSForegroundColorAttributeName : UIColor.whiteColor()]
Shamsudheen TK
  • 30,739
  • 9
  • 69
  • 102
JamesSeo
  • 3
  • 1

1 Answers1

2

You have to design a custom view with a label and assign it as the titleView of your UINavigationBar

Example Swift Code:

var titleLabel = UILabel(frame: CGRectMake(0, 0, 480, 44))
titleLabel.backgroundColor = UIColor.clearColor()
titleLabel.numberOfLines = 2
titleLabel.shadowColor = UIColor(white: 0.0, alpha: 0.5)
titleLabel.textAlignment = UITextAlignmentCenter
titleLabel.font = UIFont.boldSystemFontOfSize(12.0) 
titleLabel.textColor = UIColor.whiteColor()
titleLabel.text = "This is a\nmultiline string"
self.navigationItem.titleView = label
Shamsudheen TK
  • 30,739
  • 9
  • 69
  • 102
  • Thanks. but what I want is that a box size of label is changing dynamically as contents length of label. ( It means that the number of lines of label must be changed automatically as changing of contents length. – JamesSeo Sep 09 '15 at 14:33
  • Try to use titleLabel.intrinsicContentSize().width and modify the numberOfLines as 0 (titleLabel.numberOfLines = 0). Or just do a google search to find answer for this question https://www.google.co.in/#q=ios+UIlabel+dynamically+contents+length – Shamsudheen TK Sep 10 '15 at 05:47