underline part of uiLabel and link underlined section
Asked Answered
C

4

11

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 
Colan answered 16/7, 2015 at 7:14 Comment(1)
have a look at this SO linkSpecialistic
R
23

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
Reprehensible answered 16/7, 2015 at 7:18 Comment(5)
I've managed to underline the text. How can i make this section of text clickable?Colan
That makes it a link. I would like this link to be opened in safariColan
I replaced string URL by NSURL. If your textview is selectable it should works. Please upvote. ThxReprehensible
got an error needing fileURLWithPath added this after NSURL still does not openColan
Pierre, Great answer. Do you perhaps know how to instead of making the clickable text open a link, but rather just trigger a function ?Albano
D
5

enter image description here

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.
Donnie answered 16/7, 2015 at 7:28 Comment(0)
O
1

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
Obsession answered 13/6, 2019 at 7:14 Comment(0)
R
0

For swift3.0

  override func viewDidLoad() {
     super.viewDidLoad()

  let linkAttributes = [
        NSLinkAttributeName: NSURL(string: "http://stalwartitsolution.co.in/luminutri_flow/terms-condition")!
        ] as [String : Any]
  let attributedString = NSMutableAttributedString(string: "Please tick box to confirm you agree to our Terms & Conditions, Privacy Policy, Disclaimer. ")

  attributedString.setAttributes(linkAttributes, range: NSMakeRange(44, 18))

  attributedString.addAttribute(NSUnderlineStyleAttributeName, value: NSNumber(value: 1), range: NSMakeRange(44, 18))

  textview.delegate = self
  textview.attributedText = attributedString
  textview.linkTextAttributes = [NSForegroundColorAttributeName: UIColor.red]
  textview.textColor = UIColor.white
  }


  func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
    return true
   }
Rancidity answered 6/4, 2018 at 8:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.