Swift ios NSAttributedString Link 在 TableView 中不起作用

Swift ios NSAttributedString Link in TableView not work

extension String {    
    func accentTagAndLink(tags:Array<String>) -> NSMutableAttributedString {

        let attributedString:NSMutableAttributedString = NSMutableAttributedString(string: self)
        var NSContents = self as NSString

        for tagChild in tags {
            if !tagChild.starts(with: "<") {
                let range = NSContents.range(of: "#" + tagChild)
                var changeString = ""

                for _ in 0..<tagChild.count {
                    changeString += "$"
                }

                NSContents = NSContents.replacingOccurrences(of: tagChild, with: changeString, options: .literal, range: range) as NSString
                attributedString.addAttribute(.link, value: tagChild, range: range)
                attributedString.addAttribute(.foregroundColor, value: UIColor(red: 79/255, green: 205/255, blue: 255/255, alpha: 1), range: range)
            }
        }

        return attributedString
    }
}

tableviewCell

class PostCell: TableViewCell {
    @IBOutlet weak var postContent: UITextView!
}

在 mainViewController

class PostViewController: UIViewController, UITableViewDataSource, UITableViewDelegate,UITextViewDelegate {

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = postTableView.dequeueReusableCell(withIdentifier: "PostCell", for: indexPath) as! PostCell
        let post = postList[indexPath.row]
        let tags = (post["Content"] as! String).FindTagAtString()
        cell.postContent.delegate = self
        cell.postContent.isSelectable = true
        cell.postContent.isEditable = false
        cell.postContent.dataDetectorTypes = UIDataDetectorTypes.link
        cell.postContent.attributedText = (post["Content"] as! String).accentTagAndLink(tags: tags)
    }

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

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("cell")
    }
}

NSLinkUITextViewTableViewCell

我想要点击 Link 事件 return "link"

但只调用 TableViewCell Tab 事件

总是return"cell"

告诉我们如何使用这个我不知道

TTTAttributedLabel 有效但使我的代码崩溃

所以自己制作 Tap link

AttributedString的用法有错吗?

帮助

重要

Links in text views are interactive only if the text view is selectable but noneditable. That is, if the value of the UITextView the selectable property is YES and the isEditable property is NO.

我想你忘了将 isEditable 设置为 false。
Apple Doc