Adding underline to UILabel attributed string from the storyboard fails
Solution 1
Here is the solution in storyboard: Open the Attribute Inspector (make sure label is selected), Change the dropdown value from 'Plain' to 'Attributed'. Now a a small text editor will be visible under the font of the label. Select the text of the label, right click and change the font to 'underline'.
I also have attached a screenshot and successfully underlined a text using storyboard in XCode 7.0.1
Solution 2
Underline your text in TextEdit(cmd-U) and copy-paste it in Attribute Inspector > Label > Text > Attributed.
Solution 3
Select label -> Attribute editor -> Title = Attributed
Select the text of the label in the text view in the editor -> Right click -> Font -> check Underline
If the change is not visible -> resize the label
Solution 4
You can make text underline from storyboard like this. But when you change the text programatically it will override and underline will gone. So if you have to change the text on runtime. Do this.
For Swift 3
let text = myLabel.text
let textRange = NSMakeRange(0, (text?.characters.count)!)
let attributedText = NSMutableAttributedString(string: text!)
attributedText.addAttribute(NSUnderlineStyleAttributeName , value: NSUnderlineStyle.styleSingle.rawValue, range: textRange)
myLabel.attributedText = attributedText
For Swift 4
let text = myLabel.text
let textRange = NSRange(location: 0, length: (text?.count)!)
let attributedText = NSMutableAttributedString(string: text!)
attributedText.addAttribute(NSAttributedStringKey.underlineStyle, value: NSUnderlineStyle.styleSingle.rawValue, range: textRange)
myLabel.attributedText = attributedText
Katedral Pillon
Updated on July 05, 2022Comments
-
Katedral Pillon almost 2 years
- From the storyboard I select the UILabel in question
- Then in Attribute Inspector > Label > [I choose] Attributed
- Also in Attribute Inspector > Label > Text> [I select the content]
- Then I click on the font icon and choose underline
Basically, any change that I select from the Fonts window that pops up does not take effect.
Has anyone ever successfully add underline from the storyboard?
Note: I already know how to do this in code. I want to avoid adding code.
-
User about 9 yearsHmm. This worked first, and then all the elements on my screen just disappeared. There was no error in the console!. I undo until before pasting text from text edit, and screen was back to normal... XCode 6.2 Beta.
-
Kevin over 8 yearsFor me this only works when copy-pasting out of rich text editors. TextEdit does not do the job for me, no style is copied with the text, only the characters.
-
Kevin over 8 yearsThis is the real solution, how is this not accepted??
-
OhadM over 8 yearsUsing Xcode 7.1.1 it has a bug: when setting the underline the font shrinks by 4 and you can't change the size of the text anymore
-
Abdullah Saeed over 8 years@OhadM It is not a bug I think. When you change the text to "attributed" actually the font family changes and it seems the font has shrunk. You can change the size of the attributed text by selecting it in the small text editor in Attribute Inspector. Note that you have to select the whole text first
-
OhadM over 8 yearsI actually tried to change the size and it has no effect.
-
Gabcvit over 6 yearsWhy is this answer not marked as the accepted one? It worked perfectly for me still (2 years after the answer was written). GIVE THAT GUY A GREEN CHECKMARK ALREADY!
-
Anup Gupta about 6 yearsGood Idea :) I like your Answer.
-
Masih over 5 yearsYou deserve a beer for this +1