How to make UITextView detect links for website, mail and phone number
If you are using OS3.0
you can do it like the following
textview.editable = NO;
textview.dataDetectorTypes = UIDataDetectorTypeAll;
How to a clickable url / email / phone number on UITextView
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;
Hyperlinks in a UITextView
Use NSAttributedString
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:@"Google"
attributes:@{ NSLinkAttributeName: [NSURL URLWithString:@"http://www.google.com"] }];
self.textView.attributedText = attributedString;
Sure, you can set just a portion of the text to be the link. Please read more about the NSAttributedString
here.
If you want to have more control and do something before opening the link. You can set the delegate to the UITextView
.
- (void)viewDidLoad {
...
self.textView.delegate = self; // self must conform to UITextViewDelegate protocol
}
...
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {
// Do whatever you want here
NSLog(@"%@", URL); // URL is an instance of NSURL of the tapped link
return YES; // Return NO if you don't want iOS to open the link
}
UITextView link detection in iOS 7
It seems that in iOS 7 link detection only works if the UITextView
is selectable. So making my UITextView
not selectable stopped the the link detection from working.
I also tested this in iOS 6 and I can confirm that in iOS 6 the link detection works fine even with the UITextView
not being selectable.
Related Topics
Change Language in the App Programmatically in iOS
Is It Considered a Private API to Use App-Prefs:Root
Framework Not Found Googletoolboxformac
How to Post String with Special Character and Thai Language Using Xml Parsing in Objective C
How to Test If a String Is Empty in Objective-C
How to Disable Back Swipe Gesture in Uinavigationcontroller on iOS 7
How to Create a Uiview Bounce Animation
Dynamically Changing Font Size of Uilabel
How to Play a Sound Using Swift
How to Initialize/Instantiate a Custom Uiview Class with a Xib File in Swift
How to Hide Keyboard in Swift on Pressing Return Key
How to Assign an Action for Uiimageview Object in Swift
Xcode - Ld: Library Not Found for -Lpods
How to Detect If a Video File Was Recorded in Portrait Orientation, or Landscape in iOS
Ios9 Does Not Load Insecure Resources from a Secure Page (Ssl/Https)
Wrong Color in Interface Builder
Creating a Shadow for a Uiimageview That Has Rounded Corners