Search code examples
iosswiftoverridinguilabelnsattributedstring

How to underline a UILabel in swift?


How to underline a UILabel in Swift? I searched the Objective-C ones but couldn't quite get them to work in Swift.


Solution

  • 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
            }
        }
    }