TTTAttributedLabel中的可点击链接与Swift

Ami*_*mit 7 ios tttattributedlabel swift

我想UILabel用一些带有可点击链接的文本制作一个.不是链接到网页,而是链接到像我这样做的行为UIButton.所以我用的TTTAttributedLabel是与之完美搭配的Objective C.现在我想做同样的事情Swift,所以我写了下面的代码:

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))
Run Code Online (Sandbox Code Playgroud)

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

Har*_*kar 9

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")
        }
    }
Run Code Online (Sandbox Code Playgroud)

输出看起来像下面的截图 在此输入图像描述


Tom*_*Tom 8

String.rangeOfString返回Range,但NSString.rangeOfString返回NSRange.所以下面的代码应该工作:

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)
Run Code Online (Sandbox Code Playgroud)