underline part of uiLabel and link underlined section
You can use this also if you want to achieve only half part of label as underline:-
For Swift 4.0+
let attributesForUnderLine: [NSAttributedString.Key: Any] = [
.font: UIFont(name: AppFont.sourceSansPro_Regular, size: 12) ?? UIFont.systemFont(ofSize: 11),
.foregroundColor: UIColor.blue,
.underlineStyle: NSUnderlineStyle.single.rawValue]
let attributesForNormalText: [NSAttributedString.Key: Any] = [
.font: UIFont(name: AppFont.sourceSansPro_Regular, size: 12) ?? UIFont.systemFont(ofSize: 11),
.foregroundColor: AppColors.ColorText_787878]
let textToSet = "Want to change your preferences? Edit Now"
let rangeOfUnderLine = (textToSet as NSString).range(of: "Edit Now")
let rangeOfNormalText = (textToSet as NSString).range(of: "Want to change your preferences?")
let attributedText = NSMutableAttributedString(string: textToSet)
attributedText.addAttributes(attributesForUnderLine, range: rangeOfUnderLine)
attributedText.addAttributes(attributesForNormalText, range: rangeOfNormalText)
yourLabel.attributedText = attributedText
Please create one UILabel & check it's properties.
Please select Text on first changed it's to plain to Attributed.
Now you can seen you label text in one Textfield. select that text & right click to you mouse & goto Font menu. you can seen Underline. select it. you can seen underline in your Label.
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