I want to set a underlined navigation bar title using swift 4. Is it possible by using attributed string?
            Asked
            
        
        
            Active
            
        
            Viewed 863 times
        
    -5
            
            
        - 
                    Related: https://stackoverflow.com/questions/23602826/is-there-a-way-to-change-title-in-navigation-bar-to-italic-bold-and-underlined – Ahmad F Jun 13 '18 at 12:03
- 
                    Possible duplicate of [How to underline a UILabel in swift?](https://stackoverflow.com/questions/28053334/how-to-underline-a-uilabel-in-swift) – Do2 Jun 13 '18 at 12:03
1 Answers
0
            You can set attributed string to navigation bar title.
let navTitle = NSAttributedString(string: "My Title", attributes:
                [.underlineStyle: NSUnderlineStyle.styleSingle.rawValue])
self.title = navTitle.string
If navigationController is nil then you can't set title with above code.
Alternatively, you can use custom title view in navigation bar.
let titleView = UILabel() // set other table properties
let navTitle = NSAttributedString(string: "My Title", attributes:
                [.underlineStyle: NSUnderlineStyle.styleSingle.rawValue])
titleView.attributedText = navTitle
//set title view to navigation       
self.navigationItem.titleView = titleView
 
    
    
        Mahendra
        
- 8,448
- 3
- 33
- 56
- 
                    Your first block of code won't work since you don't actually use the attributed string. – rmaddy Jun 13 '18 at 14:45
