With a non-editable UITextView, I would like to embed text like this in iOS9+:
Just click here to register
I can create a function and manipulate the text but is there a simpler way?
I see that I can use NSTextCheckingTypeLink so getting the text clickable without the 'click here' part is straightforward in Interface Builder:
Just http://example.com to register
I'm using Xcode 8 and Swift 3 if that's relevant.
The same solution for Swift 3 using extensions :
A. Add extension -
B. Add link url -
let linkUrl = "https://www.my_website.com"
C. Implement
UITextViewDelegate
in your ViewController like this -D. Add delegate method to handle tap events -
E. And finally, things to make sure for your
UITextView
under attribute inspector -Usage -
Cheers & happy coding!
You could use this simple method to add a hyperlink to any set of characters starting with tag
Set
isEditable = false
or the text view will go into text-editing mode when user taps on it.Swift 4 and later
I wanted to do the same thing and ended up just using a UIButton with the title "click here" surrounded by UILabels "just " and " to register", and then:
The same solution for Swift 4 using extensions: