使用Swift的TTTAttributedLabel中的可点击链接 [英] Clickable link in TTTAttributedLabel with Swift

查看:519
本文介绍了使用Swift的TTTAttributedLabel中的可点击链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个UILabel,其中包含一些带有可点击链接的文本.不是链接到网页,而是链接到类似于UIButton的操作.因此,我使用了TTTAttributedLabel,它与Objective C完美配合.现在我想在Swift中做同样的事情,所以我写了下面的代码:

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))

但是,我无法使链接在Swift中工作.我收到错误:最后一行Missing argument for parameter 'host' in call.

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.

推荐答案

TTTAttributedLabel 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")
        }
    }

输出结果如下面的屏幕截图所示

这篇关于使用Swift的TTTAttributedLabel中的可点击链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆