Adding underline attribute to partial text UILabel in storyboard Adding underline attribute to partial text UILabel in storyboard ios ios

Adding underline attribute to partial text UILabel in storyboard


  1. select the UILabel and go to Attribute Inspector section.
    Change the text value from plain to Attributed .

    enter image description here

  2. Select the particular part of text which you want to Underline .

    Note: If u want full text to be Underline select full text.

    enter image description here

  3. Now right click and change the font to Underline.

    enter image description here

It will Underline the text

enter image description here


Steps:-

  1. Go to TextEdit and create your UILabel text with underline.
  2. Change UILabel text to Attributed (Attributed Selector).
  3. Copy Underlined text and assign to UILabel.


Through code:

    func underLineText(text: String)-> NSMutableAttributedString           let attributedText = NSMutableAttributedString(string: text)     attributedText.addAttribute(NSMutableAttributedString.Key.underlineStyle, value: NSUnderlineStyle.single.rawValue, range: NSRange(location: 0, length: attributedText.length))          return attributedText        }

//calling of func

   yourLbl.attributedText = underLineText(text:yourLbl.text!)

List of other underline Types

 NSMutableAttributedString.Key.underlineStyle = NSUnderlineStyle.single.rawValue NSMutableAttributedString.Key.underlineStyle = NSUnderlineStyle.thick.rawValue NSMutableAttributedString.Key.underlineStyle = NSUnderlineStyle.double.rawValue NSMutableAttributedString.Key.underlineStyle = NSUnderlineStyle.patternDot.rawValue NSMutableAttributedString.Key.underlineStyle = NSUnderlineStyle.patternDash.rawValue NSMutableAttributedString.Key.underlineStyle = NSUnderlineStyle.patternDashDot.rawValue NSMutableAttributedString.Key.underlineStyle = NSUnderlineStyle.patternDashDotDot.rawValue NSMutableAttributedString.Key.underlineStyle = NSUnderlineStyle.byWord.rawValue