Clickable link in TTTAttributedLabel with Swift
Asked Answered
P

2

9

I want to make a UILabel with some text with a click-able links in it. Not links to webpages but to actions like I do with an UIButton. So I used TTTAttributedLabel which is working perfectly with Objective C. Now I want to do the same in Swift, so I wrote the below code:

self.someLabel.text = NSLocalizedString("Lost? Learn more.", comment: "")                
let range = self.someLabel.text!.rangeOfString(NSLocalizedString("Learn more", comment:""))        
self.someLabel.addLinkToURL (NSURL(string:"action://Learn more"), withRange:NSRange (range))

However, I cannot make the link work in Swift. I am getting the error: “Missing argument for parameter 'host' in call” for the last line.

Proctoscope answered 9/12, 2014 at 15:21 Comment(2)
Try NSURL(string:"action://learn-more")Anthropogenesis
It seems the issue is with NSRange. In swift it only accepts Range<String.Index>, I would have expected Range<Int>. So with hard coded range (mentioned below) I am able to compile the code but I need to make it generic. 'self.someLabel.addLinkToURL (NSURL(string: "action://learn-more"), withRange:NSRange(location: 0, length: 5))'Proctoscope
D
18

TTTAttributedLabel lable in swift 4.2

import TTTAttributedLabel

  @IBOutlet weak var attributedLable: TTTAttributedLabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        self.setup()
    }

    func setup(){
        attributedLable.numberOfLines = 0;

    let strTC = "terms and conditions"
    let strPP = "privacy policy"

    let string = "By signing up or logging in, you agree to our \(strTC) and \(strPP)"

    let nsString = string as NSString

    let paragraphStyle = NSMutableParagraphStyle()
    paragraphStyle.lineHeightMultiple = 1.2

    let fullAttributedString = NSAttributedString(string:string, attributes: [
        NSAttributedString.Key.paragraphStyle: paragraphStyle,
        NSAttributedString.Key.foregroundColor: UIColor.black.cgColor,
        ])
    attributedLable.textAlignment = .center
    attributedLable.attributedText = fullAttributedString;

    let rangeTC = nsString.range(of: strTC)
    let rangePP = nsString.range(of: strPP)

    let ppLinkAttributes: [String: Any] = [
        NSAttributedString.Key.foregroundColor.rawValue: UIColor.blue.cgColor,
        NSAttributedString.Key.underlineStyle.rawValue: false,
        ]
    let ppActiveLinkAttributes: [String: Any] = [
        NSAttributedString.Key.foregroundColor.rawValue: UIColor.blue.cgColor,
        NSAttributedString.Key.underlineStyle.rawValue: false,
        ]

    attributedLable.activeLinkAttributes = ppActiveLinkAttributes
    attributedLable.linkAttributes = ppLinkAttributes

    let urlTC = URL(string: "action://TC")!
    let urlPP = URL(string: "action://PP")!
    attributedLable.addLink(to: urlTC, with: rangeTC)
    attributedLable.addLink(to: urlPP, with: rangePP)

    attributedLable.textColor = UIColor.black;
    attributedLable.delegate = self;
}

    func attributedLabel(_ label: TTTAttributedLabel!, didSelectLinkWith url: URL!) {
        if url.absoluteString == "action://TC" {
            print("TC click")
        }
        else if url.absoluteString == "action://PP" {
            print("PP click")
        }
    }

Out put is look like below screenshot enter image description here

Dearman answered 16/7, 2018 at 12:49 Comment(2)
Hi @Hardik i want different color for terms and condition and privacy policy.could you help me?Flatfish
you need to add delegate method and attributedLable.delegate = selfDearman
G
9

String.rangeOfString returns Range, but NSString.rangeOfString returns NSRange. So the following code should work:

let name = "tomo"
let string = "My name is \(name)"
label.text = string
let nsString = string as NSString
let range = nsString.rangeOfString(name)
let url = NSURL(string: "action://users/\(name)")!
label.addLinkToURL(url, withRange: range)
Grubbs answered 9/1, 2015 at 9:28 Comment(2)
It would be preferred if you would explain what this is doing, not just leave the code :)Hamburger
I've added a description! @HamburgerGrubbs

© 2022 - 2024 — McMap. All rights reserved.