jeudi 19 avril 2018

Find Website Address in String and Convert to URL Link

I have a String which may display a website address. I would like to display the website address as a link. How would I go about doing this? Example

label.text = "Hello and welcome to stackoverflow.  The website is https://stackoverflow.com/questions/ask"

let input = label.text
    let detector = try! NSDataDetector(types: NSTextCheckingResult.CheckingType.link.rawValue)
    let matches = detector.matches(in: input!, options: [], range: NSRange(location: 0, length: (input?.utf16.count)!))

    for match in matches {
        guard let range = Range(match.range, in: input!) else { continue }
        let url = input![range]

        print(url)
    }

I can print https://stackoverflow.com/questions/ask however, I am not sure how I can turn this into a link whilst keeping my other text inside the string.

Aucun commentaire:

Enregistrer un commentaire