Getting the range of links in attributed string

好久不见. 提交于 2019-12-02 10:29:54

With:

let attributedText = htmlStyleAttributeText(text: text)!
...
textView.attributedText = attributedText

Separate the attributes:

let underlinesAttributes: [NSAttributedString.Key: Any] = [.underlineStyle: 0x15,
                                                           .underlineColor: underLineColor]

let attributes: [NSAttributedString.Key: Any] = [.font: UIFont.systemFont(ofSize: 25),
                                                 .baselineOffset: 0]

Apply the "basic ones" to the whole text:

let wholeRange = NSRange(attributedText.string.startIndex..., in: attributedText.string)
storage.addAttributes(attributes, range: wholeRange)

We now enumerate looking for the links, and apply the effect for each one found:

attributedText.enumerateAttribute(.link, in: wholeRange, options: []) { (value, range, pointee) in
    if value != nil {
        storage.addAttributes(underlinesAttributes, range: range)
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!