How to detect links in a textView text and replace them with clickable link text
09:32 18 Jun 2018

I receive a description text for an object from the backend in html like this:

"My two favorite places to indulge on my sugar cravings are Huize van Wely (located in the 
Gelderlandplein shopping mall and Beethovenstraat) and Linnick. The first is one I ........."

I would like to find a way to detect the links and only show the link text and have the link text clickable.

I have this method that successfully strips the links:

func stripLinks(text: String) -> String {
    let pattern = "([^<]+)"
    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
    }
}

But this way the textView does not detect the links anymore, so I have the plain text but you can't click the text to go to the website.

I think that I should use an attributed string to get this working but I have failed in trying to implement it.

html ios swift regex uitextview