2

I'm working on an app for fun where I receive messages from a remote web service, like Twitter. Sometimes these messages contains URL:s, and I want to make these clickable. I wonder which approach would be the best. I have tried NSLinkAttributeName but it doesn't work for iOS. Is it possible to create a transparent button and position it above the right place in the textview? And how could I do that? Or is there a better/easier way? The position and length of the url can vary.

Anders
  • 2,903
  • 7
  • 58
  • 114

3 Answers3

3

There are two good ways of going about this:

1. CoreText, or rather a wrapper around it like TTTAtributedLabel

That's the most powerful solution. It supports Datadetectortypes (though you are better off doing your own detection for performance ahead of displaying it) and it's relatively popular.

It might however affect your tableview's scrolling performance depending on your usage.

2. Dynamically placing buttons above your labels like IFTweetLabel

That's the easy solution. IFTweetLabel is basically a drop-in for UILabel and it performs really well out of the box. However, you will eventually run into its limits if you're looking to customize it too much. Not to mention that it doesn't behave perfectly 100% of the time. YMMV.

alexander
  • 78
  • 1
  • 6
2

One option is by using UIDataDetectorTypeLink. Check this link for more details.

// Create textview, centering horizontally
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 10, 320, 50)];

// Set font, background and alignment
[textView setFont:[UIFont boldSystemFontOfSize:16]];
[textView setBackgroundColor:[UIColor blackColor]];
[textView setTextAlignment:UITextAlignmentCenter];

// Unfortunately the link will show as blue even with this setting
//  [textView setTextColor:[UIColor greenColor]];

// Edit and scrolling off  
[textView setEditable:NO];
[textView setScrollEnabled:NO];

// Set data type to specify URL/link
[textView setDataDetectorTypes:UIDataDetectorTypeLink];

// Set text as URL
[textView setText:@"text.com"];  

[self.view addSubview:textView];

Some other options are UIWebview with delegates to handle touch on urls in it. And another option is fancy UILabels

For the other questions you asked, check this How to open a UITextView URL in UI Web View?, Is there a way to create custom UIDataDetectorTypes? and Change color of UITextView links with a filter?

Community
  • 1
  • 1
iDev
  • 23,310
  • 7
  • 60
  • 85
  • Thanks, UIDataDetectorTypeLink seems to work for links. But I wonder if it is possible to change the appearance of the link, or more specifically the color. And also change so it opens in a custom UIWebView and not Safari? And is it - by subclassing UITextView - possible to make custom UIDataDetectorTypes, like @mentions and #hashtags. – Anders Oct 25 '12 at 19:07
  • Check this for, http://stackoverflow.com/questions/1555294/how-to-open-a-uitextview-url-in-ui-web-view , http://stackoverflow.com/questions/2358343/is-there-a-way-to-create-custom-uidatadetectortypes, and Change color of UITextView links with a filter? .. I have updated my answer with these links. – iDev Oct 25 '12 at 19:18
0

NSLinkAttributeName is tappable with UITextView, but not with UILabel. Just note that it's for iOS 7+, and that the UITextView must be Selectable (in Storyboard).

Swift demonstration:

let attributedText = NSMutableAttributedString(string: "Hello world")
attributedText.addAttribute(.link, value: URL(string: "https://example.com")!, range: NSRange(location: 0, length: attributedText.length))
textView.attributedText = attributedText
Cœur
  • 37,241
  • 25
  • 195
  • 267