How to underline a UILabel in swift? How to underline a UILabel in swift? ios ios

How to underline a UILabel in swift?


You can do this using NSAttributedString

Example:

let underlineAttribute = [NSAttributedString.Key.underlineStyle: NSUnderlineStyle.thick.rawValue]let underlineAttributedString = NSAttributedString(string: "StringWithUnderLine", attributes: underlineAttribute)myLabel.attributedText = underlineAttributedString

EDIT

To have the same attributes for all texts of one UILabel, I suggest you to subclass UILabel and overriding text, like that:

Swift 5

Same as Swift 4.2 but: You should prefer the Swift initializer NSRange over the old NSMakeRange, you can shorten to .underlineStyle and linebreaks improve readibility for long method calls.

class UnderlinedLabel: UILabel {override var text: String? {    didSet {        guard let text = text else { return }        let textRange = NSRange(location: 0, length: text.count)        let attributedText = NSMutableAttributedString(string: text)        attributedText.addAttribute(.underlineStyle,                                    value: NSUnderlineStyle.single.rawValue,                                    range: textRange)        // Add other attributes if needed        self.attributedText = attributedText        }    }}

Swift 4.2

class UnderlinedLabel: UILabel {override var text: String? {    didSet {        guard let text = text else { return }        let textRange = NSMakeRange(0, text.count)        let attributedText = NSMutableAttributedString(string: text)        attributedText.addAttribute(NSAttributedString.Key.underlineStyle , value: NSUnderlineStyle.single.rawValue, range: textRange)        // Add other attributes if needed        self.attributedText = attributedText        }    }}

Swift 3.0

class UnderlinedLabel: UILabel {        override var text: String? {        didSet {            guard let text = text else { return }            let textRange = NSMakeRange(0, text.characters.count)            let attributedText = NSMutableAttributedString(string: text)            attributedText.addAttribute(NSUnderlineStyleAttributeName , value: NSUnderlineStyle.styleSingle.rawValue, range: textRange)            // Add other attributes if needed            self.attributedText = attributedText        }    }}

And you put your text like this :

@IBOutlet weak var label: UnderlinedLabel!        override func viewDidLoad() {        super.viewDidLoad()                label.text = "StringWithUnderLine"    }

OLD:

Swift (2.0 to 2.3):

class UnderlinedLabel: UILabel {        override var text: String? {        didSet {            guard let text = text else { return }            let textRange = NSMakeRange(0, text.characters.count)            let attributedText = NSMutableAttributedString(string: text)            attributedText.addAttribute(NSUnderlineStyleAttributeName, value:NSUnderlineStyle.StyleSingle.rawValue, range: textRange)            // Add other attributes if needed                        self.attributedText = attributedText        }    }}

Swift 1.2:

class UnderlinedLabel: UILabel {        override var text: String! {        didSet {            let textRange = NSMakeRange(0, count(text))            let attributedText = NSMutableAttributedString(string: text)            attributedText.addAttribute(NSUnderlineStyleAttributeName, value:NSUnderlineStyle.StyleSingle.rawValue, range: textRange)            // Add other attributes if needed                        self.attributedText = attributedText        }    }}


Swift 5 & 4.2 one liner:

label.attributedText = NSAttributedString(string: "Text", attributes:    [.underlineStyle: NSUnderlineStyle.single.rawValue])

Swift 4 one liner:

label.attributedText = NSAttributedString(string: "Text", attributes:    [.underlineStyle: NSUnderlineStyle.styleSingle.rawValue])

Swift 3 one liner:

label.attributedText = NSAttributedString(string: "Text", attributes:      [NSUnderlineStyleAttributeName: NSUnderlineStyle.styleSingle.rawValue])


If you are looking for a way to do this without inheritance:

Swift 5

extension UILabel {    func underline() {        if let textString = self.text {          let attributedString = NSMutableAttributedString(string: textString)            attributedString.addAttribute(NSAttributedString.Key.underlineStyle,                                          value: NSUnderlineStyle.single.rawValue,                                          range: NSRange(location: 0, length: attributedString.length))          attributedText = attributedString        }    }}

Swift 3/4

// in swift 4 - switch NSUnderlineStyleAttributeName with NSAttributedStringKey.underlineStyleextension UILabel {    func underline() {        if let textString = self.text {          let attributedString = NSMutableAttributedString(string: textString)          attributedString.addAttribute(NSUnderlineStyleAttributeName, value: NSUnderlineStyle.styleSingle.rawValue, range: NSRange(location: 0, length: attributedString.length))          attributedText = attributedString        }    }}extension UIButton {  func underline() {    let attributedString = NSMutableAttributedString(string: (self.titleLabel?.text!)!)    attributedString.addAttribute(NSUnderlineStyleAttributeName, value: NSUnderlineStyle.styleSingle.rawValue, range: NSRange(location: 0, length: (self.titleLabel?.text!.characters.count)!))    self.setAttributedTitle(attributedString, for: .normal)  }}