如何检测textView文本中的链接并将其替换为可单击的链接文本

时间:2018-06-18 14:20:07

标签: html ios swift regex uitextview

编辑: 在downvoting之前reed the ffing description!

我一直试图找到解决方案好几天。我在stackoverflow上找到了几个帖子,但没有一个能处理类似的解决方案。 在html中从后端接收对象的描述文本,如下所示:

"My two favorite places to indulge on my sugar cravings are <a 
href=\"https://huizevanwely.nl/\">Huize van Wely</a> (located in the 
Gelderlandplein shopping mall and Beethovenstraat) and <a 
href=\"http://linnick.nl/\">Linnick</a>. The first is one I ........."

我想找到一种检测链接的方法,只显示链接文本,并且链接文本可以点击。

我有这种成功剥离链接的方法:

func stripLinks(text: String) -> String {
    let pattern = "<a href=\"[^\"]+\">([^<]+)</a>"
    do {
        let regex = try NSRegularExpression(pattern: pattern,
                                            options: [])
        let stripped = regex.stringByReplacingMatches(in: text,
                                                      options: [],
                                                      range: NSRange(location: 0, length: text.count),
                                                      withTemplate: "$1")
        return stripped
    } catch {
        print("WARNING: regex failed")
        return text
    }
}

但是这样textView不再检测链接,所以我有纯文本,但是你不能点击文本去网站。

任何人都可以帮助解决一个可行的方法。我认为我应该使用一个属性字符串来使这个工作,但我没有尝试实现它。

提前致谢。

0 个答案:

没有答案
相关问题