Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

underline part of uiLabel and link underlined section

I'm new to iOS development.

I've got a label LatestInfo this has text and is meant to have a link to a website: e.g. For the latest information visit example.com/latestInfo

I want the display to underline the url example.com/latestInfo and make it clickable.

I am using Swift not Obejective-C

How can I go about doing this?

EDIT as per Pierre's request:

@IBOutlet weak var linkLabel: UITextView!
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)

linkLabel.attributedText = attributedString 
like image 997
Pindo Avatar asked Jul 16 '15 07:07

Pindo


People also ask

How do you underline UILabel?

To make the text on UILabel underlined we will need to use the NSMutableAttributedString together with NSUnderlineStyleAttributeName. The Swift code snippet below demonstrates how to create a new UILabel programmatically and then use the NSMutableAttributedString to underline the text on the label.

How do you underline text in a Swift label?

swift 3/4/5 Select button or label title as Attributed. Select range of text which you want to underline. Right click and choose Font then select underline.

How do I underline a label in Xcode?

Select the text of the label, right click and change the font to 'underline'.

How do you bold a string in Swift?

let normalFont = UIFont(name: "Dosis-Medium", size: 18) let boldSearchFont = UIFont(name: "Dosis-Bold", size: 18) self. UILabel. attributedText = addBoldText("Check again in 30 days to find more friends", boldPartsOfString: ["Check", "30 days", "find", "friends"], font: normalFont!, boldFont: boldSearchFont!)


2 Answers

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
like image 191
Pierre Avatar answered Sep 23 '22 21:09

Pierre


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
like image 22
Vipul Kumar Avatar answered Sep 20 '22 21:09

Vipul Kumar