简体   繁体   English

标签NSMutableAttributedString以编程方式迅速4

[英]Label NSMutableAttributedString programmatically swift 4

I have to make sure that I can click on the word "Privacy" in order to open a web link. 我必须确保可以单击单词“ Privacy”(隐私)以打开Web链接。 I tried with the suggestions that I found but they are old things and they do not seem to work anymore .. I do not know how I can solve the problem 我尝试了一些发现的建议,但这些建议已经过时了,它们似乎不再起作用了..我不知道如何解决该问题

    private lazy var firstTermDescriptionLabel: UILabel = {
    let label = UILabel(frame: .zero)
    let firstTermsMessage = "I Agree to the License Terms and Privacy Policy"
    var attributedString = NSMutableAttributedString.init(string: "Privacy")
    attributedString.addAttribute(.link, value: "https://www.google.com/url?q=https://www.iubenda.com/privacy-policy/58446596&sa=D&source=hangouts&ust=1528787597335000&usg=AFQjCNEPkofPxSm7TDRMvxjOjCz5cio27w", range: NSRange(location: 0, length: 7))

    label.isUserInteractionEnabled = true
    label.text = firstTermsMessage
    label.font = UIFont.systemFont(ofSize: 13, weight: .regular)
    label.textAlignment = .left
    label.numberOfLines = 0
    label.translatesAutoresizingMaskIntoConstraints = false
    label.heightAnchor.constraint(equalToConstant: 36).isActive = true
    return label
}()

在此处输入图片说明

Updated to Swift 4 更新至Swift 4

Add this extension to your project 将此扩展添加到您的项目中

extension NSMutableAttributedString {

    public func setAsLink(textToFind:String, linkURL:String) -> Bool {

        let foundRange = self.mutableString.range(of: textToFind)
        if foundRange.location != NSNotFound {
//            self.addAttribute(NSLinkAttributeName, value: linkURL, range: foundRange)
            self.addAttributes([NSAttributedStringKey.link: URL(string: linkURL)!], range: foundRange)
            return true
        }
        return false
    }
}

Change your like below: 在下面更改您的样式:

let firstTermsMessage = "I Agree to the License Terms and Privacy Policy"
        let attributedString = NSMutableAttributedString(string:firstTermsMessage)
        let linkWasSet = attributedString.setAsLink(textToFind: "Privacy", linkURL: "https://www.google.com/url?q=https://www.iubenda.com/privacy-policy/58446596&sa=D&source=hangouts&ust=1528787597335000&usg=AFQjCNEPkofPxSm7TDRMvxjOjCz5cio27w")

        if linkWasSet {
            // adjust more attributedString properties
        }
        lableAtt.attributedText = attributedString
        lableAtt.isUserInteractionEnabled = true
lableAtt.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleTapOnLabel(_:))))


 @objc func handleTapOnLabel(_ recognizer: UITapGestureRecognizer) {
        guard let text = lableAtt.attributedText?.string else {
            return
        }

        if let range = text.range(of:"terms") {
//            goToTermsAndConditions()
        } else if let range = text.range(of: "Privacy"){
            print(range)
            UIApplication.shared.open(URL(string: "https://www.google.com/url?q=https://www.iubenda.com/privacy-policy/58446596&sa=D&source=hangouts&ust=1528787597335000&usg=AFQjCNEPkofPxSm7TDRMvxjOjCz5cio27w")!, options: [:])
        }
    }

If you're up for using a bit of third-party code, TTTAttributedLabel is a widely-used “drop-in replacement for UILabel” that lets you embed a link easily! 如果您打算使用一些第三方代码,则TTTAttributedLabel是一种广泛使用的“ UILabel的直接替代品”,可让您轻松嵌入链接!

You can find it here: https://github.com/TTTAttributedLabel/TTTAttributedLabel/ 您可以在这里找到它: https : //github.com/TTTAttributedLabel/TTTAttributedLabel/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM