Search code examples
xcodetexthyperlinkclickable

Xcode create clickable text


I am trying to create an object that can have clickable text within it.

For instance: I have text displayed to the user: "Please verify the terms and conditions prior to accepting registration."

I want to make just the "terms and conditions" text show as a link. The text would be underlined an blue.

When the user clicks the text I want them to be navigated to my terms and conditions viewcontroller page. So the link is internal to the application, not an external web page.

Questions

  1. How do I display the text with only making the specific text linkable?
  2. How do I 'seque' to the ViewController page via the user clicking the linked text?

Solution

  • You can do this with TTTAttributedLabel. From the readme file:

    In addition to supporting rich text, TTTAttributedLabel allows you to automatically detect links for URLs, addresses, phone numbers, and dates, or allow you to embed your own.

    label.dataDetectorTypes = UIDataDetectorTypeAll; // Automatically detect links when the label text is subsequently changed
    label.delegate = self; // Delegate methods are called when the user taps on a link (see `TTTAttributedLabelDelegate` protocol)
    
    label.text = @"Fork me on GitHub! (http://github.com/mattt/TTTAttributedLabel/)"; // Repository URL will be automatically detected and linked
    
    NSRange range = [label.text rangeOfString:@"me"];
    [label addLinkToURL:[NSURL URLWithString:@"http://github.com/mattt/"] withRange:range]; // Embedding a custom link in a substring