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 sug开发者_如何学运维gest me a way to do this. Or suggest any other data view to be used which can make things easy
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;
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.
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!)
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.
精彩评论