Adding underline attribute to partial text UILabel in storyboard
Asked Answered
H

3

35

How can I underline partial text of UILabel using only storyboard? I am able to do this in code and I'm able to underline the entire text of a label, but not just one or two words in the string.

Hoodlum answered 1/2, 2015 at 21:21 Comment(0)
A
135
  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

Anthropolatry answered 10/6, 2015 at 7:45 Comment(1)
This answer works in iPhone 5s but does not show the underline in 6 and 6 plus devices. Is there any other wayPaver
T
2

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.
Trainload answered 29/3, 2018 at 8:21 Comment(0)
V
0

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
Vinia answered 31/1, 2020 at 15:0 Comment(1)
Question is to apply, underline in the storyboard. For coding, we have answers in #28053834Sane

© 2022 - 2024 — McMap. All rights reserved.