Search code examples
iosswifttextviewnsattributedstring

iOS swift: textView with links to another view


I'm trying to make a TextView with some test like this one:

Cras et enim ipsum. Nullam rhoncus euismod nisi at convallis. Etiam accumsan libero odio, eget malesuada ligula bibendum venenatis.

where the links bring you to an other ViewController... I think I should use attributed text to accomplish this, but I really can't figure how...

The other solution would be to use buttons surrounded by labels, but it is not elegant...

Helps?


Solution

  • Use UITextView delegate method

    textView(_:shouldInteractWith:in:interaction:)

    You can check for URL in this method where you need to sort of deeplink to another viewcontroller. Also, return false from this method for such cases.

    If the URLS can also be opened from outside the app, you may want to look at the Deeplink solutions available in iOS for better management and will give your users a deepklink functionality as well.