Search code examples
urlxamarin.iosuilabelhyperlinkdetection

Best way to display clickable links inside UILabel using Monotouch


When developing using the Objective-C language I was able to use one of these solutions (Both not available for Monotouch):

When researching for some information I could find Miguel's implementation in the TweetStation application:

But at that time, he wrote a small parser for detecting links. As I didn't find any kind of special UILabel implementation for Monotouch, I would like to know from you:

What would be the best way I should follow to present clickable links inside a UITableViewCell's UILabel text using Monotouch?


Solution

  • I've open sourced my TTTAttributedLabel bindings for MonoTouch that you can use to get links in your labels.

    Example:

    TTTAttributedLabel label = new TTTAttributedLabel ();
    label.Text = new NSString ("I love Tink");
    label.AddLinkToURL (new NSUrl ("http://tinkapp.com/"), new NSRange (7, 4));
    label.Delegate = new LabelDelegate ();
    
    ...
    
    class LabelDelegate : TTTAttributedLabelDelegate {
        public override void DidSelectLinkWithURL (TTTAttributedLabel label, NSUrl url)
        {
            Console.WriteLine ("Clicked URL: {0}", url.ToString ());
        }
    }