【问题标题】:How to detect if a link, inside a UITextView inside a TabelCell has been clicked, Swift如何检测是否点击了 TabelCell 内的 UITextView 内的链接,Swift
【发布时间】:2018-11-26 02:48:51
【问题描述】:

我的目标是记录用户点击的所有链接。 链接嵌入在表格单元格内的 UITextView 中,当用户按下链接时,我希望它调用打印 url 的函数。

我看过类似的问题,例如 How to detect if a link, inside a UITextView has been clicked, SwiftHow to intercept click on link in UITextView? 人们建议使用函数shouldInteractWithURL,但该函数从未被调用。

我想我可能将委托或函数设置在错误的位置,如果有人能告诉我应该如何设置,那就太好了。

以下是部分代码和页面截图:

class BSPdetailViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextViewDelegate {


    func textView(textView: UITextView!, shouldInteractWithURL URL: NSURL!, inRange characterRange: NSRange) -> Bool {
        print("Link Selected!")
        return true

    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "detailCell", for: indexPath as IndexPath) as! detailCell
        cell.label?.text = ""


        cell.detailLinks.delegate = self
        cell.detailLinks.isUserInteractionEnabled = true // default: true
        cell.detailLinks.isEditable = false // default: true
        cell.detailLinks.isSelectable = true // default: true
        cell.detailLinks.dataDetectorTypes = [.link]

        cell.detailTextLabel?.font = UIFont.fontAwesome(ofSize: 20)
        cell.detailTextLabel?.text = ""
        cell.detailTextLabel?.numberOfLines = 0;


        cell.detailLinks?.text = "links are here"
        var linkString = NSMutableAttributedString(string: "", attributes: [NSFontAttributeName:UIFont.systemFont(ofSize: 22.0)])
        let video =  String.fontAwesomeIcon(code: "fa-video-camera")! + "  "
        let document = String.fontAwesomeIcon(code: "fa-file-text")! + "\n\n"

        for i in 0..<stratList.count {
            var stratURL = stratList[i].lowercased().replacingOccurrences(of: " ", with: "-")

            var docLink = "http://" + stratURL
            var videoLink = "http://" + stratURL

            //setting the url
            var attributedString = NSMutableAttributedString(string: video, attributes: [NSFontAttributeName: UIFont.fontAwesome(ofSize: 22)])
            attributedString.addAttribute(NSLinkAttributeName, value: docLink, range: NSRange(location: 0, length: 1))
            linkString.append(attributedString as NSAttributedString)

            attributedString = NSMutableAttributedString(string: document, attributes: [NSFontAttributeName: UIFont.fontAwesome(ofSize: 22)])
            attributedString.addAttribute(NSLinkAttributeName, value: videoLink, range: NSRange(location: 0, length: 1))
            linkString.append(attributedString as NSAttributedString)


            cell.label?.text = (cell.label?.text)! + "• " + stratList[i] + "\n\n"


            cell.detailLinks?.attributedText = linkString

            //tried tapgesture but it didn't work when I tapped on the links
            //cell.detailLinks.addGestureRecognizer(UITapGestureRecognizer(target: cell.detailLinks, action: #selector(cell.detailLinks.singleTap(tap:))))

            cell.backgroundColor = contentColors[rowNo]
            cell.label?.textColor = UIColor.black
        }


        return cell
    }
}

class detailCell: UITableViewCell
{

    @IBOutlet weak var detailLinks: UITextView!
    @IBOutlet weak var label: UILabel!

}

结果表:

【问题讨论】:

  • UITextViewDelegate 方法 shouldInteractWithURL 应该写在 cellForRowAt 外面而不是里面。
  • @GovindKumawat 谢谢!我试过了,这个函数仍然没有被调用。还有其他建议吗?

标签: swift uitableview url uitextview uitextviewdelegate


【解决方案1】:

UITextViewDelegate 方法shouldInteractWithURL 应该写在cellForRowAt 之外而不是里面。您的标准 UITextView 设置应如下所示,不要忘记委托和 dataDetectorTypes。

    cell.detailLinks.delegate = self
    detailLinks.isUserInteractionEnabled = true // default: true
    detailLinks.isEditable = false // default: true
    detailLinks.isSelectable = true // default: true
    detailLinks.dataDetectorTypes = [.link]

UITextViewDelegate 方法shouldInteractWithURL:

func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
    print("Link Selected!")
    return true
}

【讨论】:

  • 再次感谢!我已经尝试过这些,但仍然没有调用该函数。我已更新代码以反映您的建议。
  • @haedaldal 这里的问题是您的UITextViewDelegate 方法shouldInteractWithURL 您使用了错误的方法,其中缺少一个参数,也许它在以前的快速版本中使用过。
  • 这真的救了我。我显然从其他地方复制了旧版本的 shouldInteractWithUrl 函数,但无法让它工作。现在终于搞定了!
猜你喜欢
  • 1970-01-01
  • 2019-04-12
  • 1970-01-01
  • 2011-05-23
  • 2013-02-23
  • 1970-01-01
  • 2018-04-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多