Hyperlinks in a UITextView

I am trying to create a UITextView with hyperlink so that when the user clicks on the link, his UITextView in safari to open the web page. I read about link detectors for viewing textview but these examples always show how link definition works if the text contains an actual link (for example, www.google.com ). I want it to be plain text, which when clicked opens the linked URL. (i.e. Google is text, and when clicked, the URL www.google.com opens). How can I do this in iOS7 / 8?

+11
source share
5 answers

Use NSAttributedString

 NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:@"Google" attributes:@{ NSLinkAttributeName: [NSURL URLWithString:@"http://www.google.com"] }]; self.textView.attributedText = attributedString; 

Of course, you can set only part of the text as a link. Read more about NSAttributedString here .

If you want to get more control and do something before opening the link. You can set the delegate to a 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 } 
+16
source

Swift 3, iOS10, Xcode 9

@Sikhapol's answer is really good if you know exactly the words you want to parse, such as a dictionary of words.

it's all about the string itself, which is displayed in a UITextView

My solution is based on text rendering, if you make UITextView display HTML tags, you can use href tag.

Here are some code links you can use

First you need to configure a UITextView from the interface constructor or form code for

  1. The choice
  2. Detector Data

Note: do not make the text view editable

Interface constructor

enter image description here

programmatically

  let htmlData = NSString(string: "go to <a href=\"http://www.google.com\">google</a> and search for it").data(using: String.Encoding.unicode.rawValue) let attributedString = try! NSAttributedString(data: htmlData!, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil) yourUIViewView.isSelectable = true yourUIViewView.dataDetectorTypes = .link yourUIViewView.attributedText = attributedString yourUIViewView.delegate = self 

for UITextViewDelegate

  func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool { // check for the url string for performing your own custom actions here let urlString = URL.absoluteString // Return NO if you don't want iOS to open the link return true } 
+8
source

if you want to use the active substring in your UITextView, you can use my advanced TextView ... its short and simple. You can edit it as you wish.

how to use (range = substring location):

 [self.textView addTapActionWithRange:range withActionBlock:^{ // anything you want to do - show something }]; 

Result: enter image description here

source code: https://github.com/marekmand/ActiveSubstringTextView

0
source

In this code example, there are two different links on the same label, and the URL color is set to avoid blue by default.

 UITextView * textTerm = [UITextView new]; NSMutableAttributedString *attrRight = [[NSMutableAttributedString alloc] initWithString:@"Terms of Service" attributes:@{ NSLinkAttributeName: [NSURL URLWithString:@"http://www.google.com"] }]; NSMutableAttributedString *attrLeft = [[NSMutableAttributedString alloc] initWithString:@"Privacy Policy" attributes:@{ NSLinkAttributeName: [NSURL URLWithString:@"http://www.google.com"] }]; [attrRight appendAttributedString:attrLeft]; textTerm.attributedText = attrRight; textTerm.editable = NO; textTerm.dataDetectorTypes = UIDataDetectorTypeAll; textTerm.linkTextAttributes = [UIColor whiteColor]; textTerm.backgroundColor = [UIColor clearColor]; 
0
source

An elegant little extension that I wrote and use (Swift 4.2, tested on iOS 12.1)

 extension NSAttributedString { func replace(placeholder: String, with hyperlink: String, url: String) -> NSAttributedString { let mutableAttr = NSMutableAttributedString(attributedString: self) let hyperlinkAttr = NSAttributedString(string: hyperlink, attributes: [NSAttributedString.Key.link: URL(string: url)!]) let placeholderRange = (self.string as NSString).range(of: placeholder) mutableAttr.replaceCharacters(in: placeholderRange, with: hyperlinkAttr) return mutableAttr } } 

Using:

 //Set through code or through interface builder footerText.isSelectable = true footerText.dataDetectorTypes = .link //Keeps the original formatting from xib or storyboard footerText.text = "By continuing, you are indicating that you accept our @ Terms@ and @ Privacy@. " footerText.attributedText = footerText.attributedText? .replace(placeholder: "@ Terms@ ", with: "Terms and Conditions", url: AppUrl.terms) .replace(placeholder: "@ Privacy@ ", with: "Privacy Policy", url: AppUrl.privacy) 
0
source

Source: https://habr.com/ru/post/1206991/


All Articles