Umang
Umang

Reputation: 139

How to a clickable url / email / phone number on UITextView

I am receiving a server response as a sting that can contain some url or email address or phone number ie same string can have all three of them or one number and couple of url. Nothing is fixed.

I am using UITextView to show this sting on device. Now what I want is these url/email/phone number show up as hyperlink and on clicking these hyperlink user should be able to see a webpage if its a url, draft and send an email if its email address or call the number when clicked on number hyper link

Can anyone suggest me a way to do this. Or suggest any other data view to be used which can make things easy

Upvotes: 9

Views: 13598

Answers (4)

TimJowers2
TimJowers2

Reputation: 228

Add a UITextView in Interface Builder. No code needed. In Attributes Inspector, uncheck "Editable" and do check "Selectable". Do check "Phone Number" beside Data Detectors. Should work for other data types. DO be sure to edit the "Hint color" or else it may appear as no label is there (but you can still touch to launch). (Its a beautiful world, if you look at it the right way!)

Upvotes: 3

Ganesh S Bhat
Ganesh S Bhat

Reputation: 237

Also note that the textview should not be editable when you use NSDataDetectorTypes.

Let textView be an object for UITextView, then set:

textView.editable = NO;

then only it can detect the links, phone numbers, address etc.

Upvotes: 6

MarkPowell
MarkPowell

Reputation: 16540

Set the data detector types of your UITextView. There are a number of options:

UIDataDetectorTypePhoneNumber

UIDataDetectorTypeLink

UIDataDetectorTypeAddress

UIDataDetectorTypeCalendarEvent

UIDataDetectorTypeNone

UIDataDetectorTypeAll

So, in your case, you'd probably like to do:

self.myTextView.dataDetectorTypes = UIDataDetectorTypeAll;

Upvotes: 20

Joetjah
Joetjah

Reputation: 6132

You could use an UIButton, and change the title into the URL, email or phone number.

Create a custom UIButton with the corresponding format to make it look like a normal sentence (the edges and background of the button have the same color as the background of the view).

Format the text to make it look blue (and optionally underlined) like this so people know it's a hyperlink.

Upvotes: 0

Related Questions