【问题标题】:Label NSMutableAttributedString programmatically swift 4以编程方式快速标记 NSMutableAttributedString 4
【发布时间】:2018-06-11 08:55:42
【问题描述】:

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

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

【问题讨论】:

  • 你在哪里设置属性字符串?
  • @karthikeyan " var attributesString = NSMutableAttributedString.init(string: "Privacy") attributesString.addAttribute(.link, value: "google.com/url?q=https://www.iubenda.com/privacy-policy/…", range: NSRange(location: 0, length: 7 ))"
  • 不要使用UILabel,使用UITextView。 Cf here at 2:00 developer.apple.com/videos/play/wwdc2018/221 “选择正确的控制”部分。
  • @Larme 好的,我替换了,如何将链接添加到单词? “attributedString”方法似乎一直没有看到我..
  • Textview.attributedText = 属性字符串,而不是 .text

标签: ios iphone swift xcode uilabel


【解决方案1】:

更新到 Swift 4

将此扩展添加到您的项目中

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

在下面更改您的喜欢:

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: [:])
        }
    }

【讨论】:

  • @StefanoOddone 如果它没有在 safari 中打开。控制台中的错误是什么
  • @Vinodh 我必须使用“NSMutableAttributedString”而不是这个过程。我也按照建议尝试了这个,但它给了我太多的冲突,解决它们会使应用程序崩溃。
【解决方案2】:

如果您愿意使用一些第三方代码,TTTAttributedLabel 是一种广泛使用的“UILabel 替代品”,可让您轻松嵌入链接!

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

【讨论】:

    猜你喜欢
    • 2015-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-11
    • 2016-07-27
    • 2017-06-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多