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.
Asked
Active
Viewed 2.3k times
35
3 Answers
135
select the
UILabel
and go to Attribute Inspector section.
Change the text value from plain to Attributed .Select the particular part of text which you want to Underline .
Note: If u want full text to be Underline select full text.
Now right click and change the font to Underline.
It will Underline the text

Rizwan Shaikh
- 2,824
- 2
- 27
- 49
-
This answer works in iPhone 5s but does not show the underline in 6 and 6 plus devices. Is there any other way – Kautham Krishna Jun 04 '16 at 16:58
2
Steps:-
- Go to TextEdit and create your UILabel text with underline.
- Change UILabel text to Attributed (Attributed Selector).
- Copy Underlined text and assign to UILabel.

Manish Mahajan
- 2,062
- 1
- 13
- 19
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

AyAz
- 2,027
- 2
- 21
- 28
-
Question is to apply, underline in the storyboard. For coding, we have answers in https://stackoverflow.com/questions/28053334/how-to-underline-a-uilabel-in-swift – Vinoth Anandan May 10 '21 at 06:00