UITableViewCell with selectable/copyable text that also detects URLs on the iPhone

Posted by Jasarien on Stack Overflow See other posts from Stack Overflow or by Jasarien
Published on 2010-03-25T11:17:12Z Indexed on 2010/03/25 11:33 UTC
Read the original article Hit count: 429

Hi guys,

I have a problem. Part of my app requires text to be shown in a table. The text needs to be selectable/copyable (but not editable) and any URLs within the text need to be highlighted and and when tapped allow me to take that URL and open my embedded browser.

I have seen a couple of solutions that solve one of either of these problems, but not both.

Solution 1: Icon Factory's IFTweetLabel

The first solution I tried was to use the IFTweetLabel class made possible by Icon Factory and used in Twitterrific.

While this solution allows for links (or anything you can find with a regex) to be detected to be handled on a case by case basis, it doesn't allow for selecting and copying.

There is also an issue where if a URL is long enough to be wrapped, the button that the class overlays above the URL to make it interactive cannot wrap and draws off screen, looking very odd.

Solution 2: Use IFTweetLabel and handle copy manually

The second thing I tried was to keep IFTweetLabel in place to handle the links, but to implement the copying using a long-tap gesture, like how the SMS app handles it. This was just about working, but it doesn't allow for arbitrary selection of text, the whole text is copied, or none is copied at all... Pretty black and white.

Solution 3: UITextView

My third attempt was to add a UITextView as a subview of the table cell.

The only thing that this doesn't solve is the fact that detected URLs cannot be handled by me. The text view uses UIApplication's openURL: method which quits my app and launched Safari.

Also, as the table view can get quite large, the number of UITextViews added as subviews cause a noticeable performance drag on scrolling throughout the table, especially on iPhone 3G era devices (because of the creation, layout, compositing whenever a cell is scrolled on screen, etc).

So my question to all you knowledgeable folk out there is: What can I do?

Would a UIWebView be the best option? Aside from a performance drag, I think a webview would solve all the above issues, and if I remember correctly, back in the 2.0 days, the Apple documentation actually recommended web views where text formatting / hyperlinks were required.

Can anyone think of a way to achieve this without a performance drag?

Many thanks in advance to everyone who can help.

© Stack Overflow or respective owner

Related posts about iphone

Related posts about uitableviewcell