I'm trying to programmatically add a link to the last part of a UILabel, but not have the entire UILabel become a link (ignore the ranges as I changed the input string).
let mutableString = NSMutableAttributedString(string: "This is a string. This is the link that should be underlined")
mutableString.addAttribute(NSLinkAttributeName, value: "www.website.com", range: NSMakeRange(57, 16))
self.label.attributedText = mutableString
The above code does what I want functionally, but now the link is underlined, which is not what I want. So I added the following line:
mutableString.addAttribute(NSUnderlineColorAttributeName, value: NSUnderlineStyle.StyleNone.rawValue, range: NSMakeRange(57, 16))
Now the text in the range won't appear at all. Any ideas as to how to make this work?
Try this
let mutableString = NSMutableAttributedString(string: "This is a string. This is the link that should be underlined")
mutableString.addAttribute(NSLinkAttributeName, value: "www.website.com", range: NSMakeRange(0, 16))
mutableString.addAttribute(NSUnderlineStyleAttributeName, value: NSUnderlineStyle.StyleNone.rawValue, range: NSMakeRange(0, 16))
mutableString.addAttribute(NSUnderlineColorAttributeName, value: UIColor.clearColor(), range: NSMakeRange(0, 16))

I hope this helps you, for me works like charm
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With