It's trivial to make hyperlinks clickable in a UITextView
. You just set the "detect links" checkbox on the view in IB, and it detects HTTP links and turns them into hyperlinks.
However, that still means that what the user sees is the "raw" link. RTF files and HTML both allow you to set up a user-readable string with a link "behind" it.
It's easy to install attributed text into a text view (or a UILabel
or UITextField
, for that matter.) However, when that attributed text includes a link, it is not clickable.
Is there a way to make user-readable text clickable in a UITextView
, UILabel
or UITextField
?
The markup is different on SO, but here is the general idea. What I want is text like this:
This morph was generated with Face Dancer, Click to view in the app store.
The only thing I can get is this:
This morph was generated with Face Dancer, Click on http://example.com/facedancer to view in the app store.
I found this really useful but I needed to do it in quite a few places so I've wrapped my approach up in a simple extension to
NSMutableAttributedString
:Swift 3
Swift 2
Example usage:
Objective-C
I've just hit a requirement to do the same in a pure Objective-C project, so here's the Objective-C category.
Example usage:
I needed to keep using a pure UILabel, so called this from my tap recognizer (this is based on malex's response here: Character index at touch point for UILabel )
The excellent library from @AliSoftware
OHAttributedStringAdditions
makes it easy to add links inUILabel
here is the documentation: https://github.com/AliSoftware/OHAttributedStringAdditions/wiki/link-in-UILabelSwift 4:
Swift 3.1:
I have written a method, that adds a link(linkString) to a string (fullString) with a certain url(urlString):
You should call it like this:
if you want active substring in your UITextView then you can use my extended TextView... its short and simple. You can edit it as you want.
result:
code: https://github.com/marekmand/ActiveSubstringTextView