I have a UITextView object. The text in UIView has a phone number, mail link, a website link. I want to show them as links with following functionality.
When someone
I'm curious, do you have control over the text shown? If so, you should probably just stick it in a UIWebView and throw some links in there to do it "the right way".
If you are using OS3.0
you can do it like the following
textview.editable = NO;
textview.dataDetectorTypes = UIDataDetectorTypeAll;
Swift 4.2 Xcode 10.1
func setupContactUsTextView() {
let text = NSMutableAttributedString(string: "Love your App, but need more help? Text, Call (123) 456-1234 or email ")
if let font = UIFont(name: "Calibri", size: 17) {
text.addAttribute(NSAttributedStringKey.font,
value: font,
range: NSRange(location: 0, length: text.length))
} else {
text.addAttribute(NSAttributedStringKey.font,
value: UIFont.systemFont(ofSize: 17),
range: NSRange(location: 0, length: text.length))
}
text.addAttribute(NSAttributedStringKey.foregroundColor,
value: UIColor.init(red: 112/255, green: 112/255, blue: 112/255, alpha: 1.0),
range: NSRange(location: 0, length: text.length))
text.addAttribute(NSAttributedStringKey.link, value: "tel://", range: NSRange(location: 49, length: 15))
let interactableText = NSMutableAttributedString(string: "contact@abc.com")
if let font = UIFont(name: "Calibri", size: 17) {
interactableText.addAttribute(NSAttributedStringKey.font,
value: font,
range: NSRange(location: 0, length: interactableText.length))
} else {
interactableText.addAttribute(NSAttributedStringKey.font,
value: UIFont.systemFont(ofSize: 17),
range: NSRange(location: 0, length: interactableText.length))
}
interactableText.addAttribute(NSAttributedStringKey.link,
value: "contact@abc.com",
range: NSRange(location: 0, length: interactableText.length))
interactableText.addAttribute(NSAttributedStringKey.underlineStyle,
value: NSUnderlineStyle.styleSingle.rawValue,
range: NSRange(location: 0, length: interactableText.length))
text.append(interactableText)
videoDescTextView.attributedText = text
videoDescTextView.textAlignment = .center
videoDescTextView.isEditable = false
videoDescTextView.isSelectable = true
videoDescTextView.delegate = self
}
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool {
if (characterRange.location > 48 && characterRange.location < 65){
print("open phone")
}else{
print("open gmail")
}
return false
}
Steps - 1. Set the delegate to your text field and don't forget to implement UITextViewDelegate 2. Take the textView outlet - @IBOutlet weak var videoDescTextView: UITextView! 3. Add these two functions given above. This function shows how to detect phone numbers, email from textView, how to underline your email id, how to give custom color to your text, custom font, how to call a function when tapping on phone or email, etc.
Hope this will help someone to save their valuable time. Happy Coding :)
Step 1. Create a subclass of UITextview and override the canBecomeFirstResponder function
KDTextView.h Code:
@interface KDTextView : UITextView
@end
KDTextView.m Code:
#import "KDTextView.h"
// Textview to disable the selection options
@implementation KDTextView
- (BOOL)canBecomeFirstResponder {
return NO;
}
@end
Step 2. Create the Textview using subclass KDTextView
KDTextView*_textView = [[KDTextView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[_textView setScrollEnabled:false];
[_textView setEditable:false];
_textView.delegate = self;
[_textView setDataDetectorTypes:UIDataDetectorTypeAll];
_textView.selectable = YES;
_textView.delaysContentTouches = NO;
_textView.userInteractionEnabled = YES;
[self.view addSubview:_textView];
Step 3: Implement the delegate method
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange
{
return true;
}
As of swift 3.0, use the following code if you want to do it programmatically.
textview.isEditable = false
textview.dataDetectorTypes = .all
Or if you have a storyboard
A note on detecting email addresses: The Mail app must be installed (it's not on the iOS Simulator) for email links to open a message compose screen.