You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改ReadMoreTextView pod默认行为,避免其阻止链接检测功能

问题原因

ReadMoreTextView开启截断功能(shouldTrim = true)时,内部会对原始文本做二次加工:自动计算截断位置,拼接“展开/收起”自定义后缀生成新的属性文本,直接赋值给当前textView的attributedText。这个过程会覆盖掉系统基于dataDetectorTypes自动生成的链接属性,且库本身的实现逻辑里没有对加工后的文本重新执行系统链接检测,所以你设置的dataDetectorTypes = [.link]不会生效。

解决方案

可以通过覆写库的默认逻辑解决,参考实现如下:

  1. 首先在给textView赋值前,主动对原始文本做链接检测,生成带链接属性的属性文本再传入
  2. 扩展ReadMoreTextView,修改其截断拼接逻辑,保留原有文本的属性
    示例代码:
extension ReadMoreTextView {
    // 对外提供带属性文本的赋值方法
    func setTextWithLinkDetection(_ rawText: String) {
        let attributedString = NSMutableAttributedString(string: rawText)
        // 手动执行链接检测
        let detector = try! NSDataDetector(types: NSTextCheckingResult.CheckingType.link.rawValue)
        let matches = detector.matches(in: rawText, range: NSRange(location: 0, length: rawText.utf16.count))
        for match in matches {
            guard let range = Range(match.range, in: rawText), let url = match.url else { continue }
            attributedString.addAttribute(.link, value: url, range: NSRange(range, in: rawText))
        }
        // 赋值给库的attributedText,确保内部截断时保留属性
        self.attributedText = attributedString
    }
}

如果需要完全兼容原有调用方式,也可以直接覆写库内的updateTrimText()方法,在生成截断后属性文本的逻辑中增加上述链接检测步骤即可。

内容的提问来源于stack exchange,提问作者kunass2

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.11 21:45:03