ios - TableView 中的 Swift ios NSAttributedString 链接不起作用

标签 ios swift uitableview nsattributedstring

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

表格 View 单元格

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")
    }
}
TableViewCell 中的UITextView 中的

NSLink

我想点击链接事件返回“链接”

但只调用TableViewCellTab事件

总是返回“单元格”

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

TTTAtributedLabel 可以正常工作,但我的代码崩溃了

所以自己做点击链接

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

关于ios - TableView 中的 Swift ios NSAttributedString 链接不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51941411/

相关文章:

ios - Xcode: EXC_BREAKPOINT (EXC_ARM_BREAKPOINT, subcode=0xe7ffdefe)

ios - 根据内容的大小调整文本标签的大小

ios - 如何将 NSIndexPath 转换为 NSString-iOS

ios - 如何使用 SpriteKit 的 SKAction 为 SKSpriteNode 创建时间间隔?

ios - 尝试将 UIGraphicsImageRenderer 与 SCNView 一起使用会产生空结果

swift - 数组元素分配导致昂贵的 _ArrayBuffer Protocol.init(copying :)

iOS 在展开可选值时意外发现 nil

ios - 自动调整表格 View 单元格内的自动调整表格 View 大小

ios - 使用 initWithContentsOfURL 使用 iPod 歌曲 URL 初始化 AVAudioPlayer

ios - 解析 saveInBackgroundWithBlock 在 iOS 上崩溃