【问题标题】:Trigger hyper link text click and and normal text click in UITextView在 UITextView 中触发超链接文本单击和普通文本单击
【发布时间】:2019-12-09 08:10:18
【问题描述】:

您好,我正在尝试为UITextView 中的超链接点击和普通文本点击触发单独的点击事件

如何区分UITextView中的超链接文本点击和普通文本点击

希望你能理解我的问题。

这是我尝试过的。

     override func viewDidLoad() {
        super.viewDidLoad()
        let atributedHtmlText = """
            Hey I dont have hyper link text so trigger func A(). Click <a href="http://www.google.com">I have hyper link so trigger func b here</a> for more information.
            """
        testTV.setAttributedHtmlText(atributedHtmlText)

        let tap = UITapGestureRecognizer(target: self, action: #selector(ViewController.tapFunction))
        testTV.isUserInteractionEnabled = true
        testTV.addGestureRecognizer(tap)
        testTV.delegate = self
     }

     @objc func tapFunction(sender:UITapGestureRecognizer) {
        print("tap working")
     }

     extension ViewController: UITextViewDelegate {
        func textView(_ textView: UITextView, shouldInteractWith URL: URL,
                  in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
          print("URL Text Tapped ", URL)
          return false
    }
}

【问题讨论】:

  • 这能回答你的问题吗? How to intercept click on link in UITextView?
  • 感谢您的回复。但这不是我想要的。例如,我想在单击超链接文本时触发函数 A(应该InteractWith URL 工作正常),我想在单击 UITextView 中的其他文本时触发函数 B
  • 您可以添加一个“虚假 URL”(在 NSAttributedStringKey.Link 属性下)并检查在 textView(_ textView:shouldInteractWith:) 中点击的 URL 是什么,以了解该怎么做。

标签: ios swift uitextview nsattributedstring


【解决方案1】:

定义一个属性字符串,然后通过定义您想要点击以打开链接的范围向其添加属性。并且不要忘记添加textView的委托方法,并且还要在视图中加载YourTextView.delegate = self

    class ViewController: UIViewController, UITextViewDelegate {
    @IBOutlet var textView: UITextView!

    override func viewDidLoad() {
        textView.delegate = self.
        let attributedString = NSMutableAttributedString(string: "want to search something on Google! Click Here")
        attributedString.addAttribute(.link, value: "www.google.com", range: NSRange(location: 36, length: 10))

        textView.attributedText = attributedString
    }

    func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
        UIApplication.shared.open(URL)
        return false
    }
}

【讨论】:

  • 感谢您的回答。但这不是我要找的。例如,我想在单击超链接文本时触发函数 A(应该与 URL 正常工作),我想在单击 UITextView 中的其他文本时触发函数 B。
【解决方案2】:

一种可能的解决方案是在没有链接属性的情况下添加“假 URL”(如“内部 URL 方案相似):

attributedText.enumerateAttribute(.link, in: NSRange(location: 0, length: attributedText.length), options: []) { (attribute, range, pointee) in
    //If there is no URL => Set our custom one
    if attribute == nil {
        attributedText.addAttribute(.link, value: "com.myapp.custom", range: range)
    }
}

在这个委托方法中,检查URL的值。

func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
    print("url: \(URL)")
    if url == ... {} else { ... }
    return false
}

【讨论】:

    猜你喜欢
    • 2015-11-28
    • 1970-01-01
    • 1970-01-01
    • 2018-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-27
    相关资源
    最近更新 更多