How to Underline a Uilabel in Swift

How to underline a UILabel in swift?

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

underline part of uiLabel and link underlined section

Look for NSMutableAttributedString and especially for NSLinkAttributeName. There're lots of tutorials and Stackoverflow questions about that. You can also read Apple's documentation about attributed string
TextView is the onlycomponent able to open links. So just replace your label with that and :

let string              = "A great link : Google"
let range = (string as NSString).rangeOfString("Google")
let attributedString = NSMutableAttributedString(string: string)

attributedString.addAttribute(NSLinkAttributeName, value: NSURL("http://www.google.fr")!, range: range)
attributedString.addAttribute(NSUnderlineStyleAttributeName, value: NSNumber(int: 1), range: range)
attributedString.addAttribute(NSUnderlineColorAttributeName, value: UIColor.orangeColor(), range: range)


textView.attributedText = attributedString

How to make an underlined text in UILabel?


Objective-C

iOS 6.0 > version

UILabel supports NSAttributedString

NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:@"Hello Good Morning"];
[attributeString addAttribute:NSUnderlineStyleAttributeName
value:[NSNumber numberWithInt:1]
range:(NSRange){0,[attributeString length]}];

Swift

let attributeString: NSMutableAttributedString =  NSMutableAttributedString(string: "Hello Good Morning")
attributeString.addAttribute(NSUnderlineStyleAttributeName, value: 1, range: NSMakeRange(0, attributeString.length))

Definition :

- (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)aRange

Parameters List:

name : A string specifying the attribute name. Attribute keys can be supplied by another framework or can be custom ones you define. For information about where to find the system-supplied attribute keys, see the overview section in NSAttributedString Class Reference.

value : The attribute value associated with name.

aRange : The range of characters to which the specified attribute/value pair applies.

Now use like this:

yourLabel.attributedText = [attributeString copy];

iOS 5.1.1 < version

You needs 3 party attributed Label to display attributed text:

1) Refer TTTAttributedLabel link. Its best third party attributed Label to display attributed text.

2) refer OHAttributedLabel for third party attributed Label

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 .

    Sample Image

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

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

    Sample Image

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

    Sample Image

It will Underline the text

Sample Image

How to Create Label Doted Underline in Swift

//you can add doted underline string like this in swift 5

let string = NSMutableAttributedString(string: "Hello string")
string.addAttributes([.underlineStyle : NSUnderlineStyle.single.union(.patternDot).rawValue],
range: NSRange(location: 0, length: string.length))

yourLableName.attributedText = string


Related Topics



Leave a reply



Submit