首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >以编程方式标记NSMutableAttributedString 4

以编程方式标记NSMutableAttributedString 4
EN

Stack Overflow用户
提问于 2018-06-11 08:55:42
回答 2查看 839关注 0票数 0

我必须确保我可以点击“隐私”这个词,以便打开一个网页链接。我试着用我发现的建议,但它们都是老东西,它们似乎不再起作用了。我不知道如何解决这个问题。

代码语言:javascript
运行
复制
    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
}()

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-06-11 09:18:02

更新为Swift 4

将此扩展添加到项目中

代码语言:javascript
运行
复制
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
    }
}

改变你的如下方式:

代码语言:javascript
运行
复制
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: [:])
        }
    }
票数 0
EN

Stack Overflow用户

发布于 2018-06-11 17:17:04

如果您准备使用一些第三方代码,TTTAttributedLabel是一个广泛使用的“插入替代UILabel”,它可以让您轻松地嵌入链接!

你可以在这里找到它:https://github.com/TTTAttributedLabel/TTTAttributedLabel/

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50793833

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档