views:

397

answers:

2

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.

A: 

As soon as I hit the submit button, a new idea hit me.

I was so preoccupied with having URLs inline with text and interactive that I didn't consider that maybe it's not the best solution.

I'm certain that to achieve that kind of behaviour, a UIWebView is the best choice, regardless of the performance issues.

However, maybe a better user experience / interaction is to not highlight the URLs inline, but to gather them into an array behind the scenes, and present a disclosure button as the cell's accessory view?

Then for selection and copying text, I could just use the UITextView with data detectors turned off and not worry about the links being sent off to safari and closing my app.

When the disclosure button is tapped, the user could be whisked off to the URL found in the text, or if more than one URL is found, present the user with a picker view to choose which to go to.

Any thoughts/criticisms of this idea are welcome.

Jasarien
Are you thinking of using a UIWebView inside of each cell (I tried this before, didn't work well) or just a UIWebView instead of a tableview altogether (interested to hear how this works, I want to try this eventually)?
bpapa
I was thinking a `UIWebView` in each cell. What issues did you run into? Was it just that the performance was unacceptable?
Jasarien
Check out this for more - http://stackoverflow.com/questions/646809/how-do-i-use-a-uiwebview-in-a-table-cell
bpapa
@bpapa, it's kind of ironic that one of the answers to your question about UIWebViews within table cells is from me.... ;)
Jasarien
A: 

You can prevent a textfield from being edited by overriding the UITextField Delegate methods such that they do not apply any edits. That leaves the field selectable and copyable but prevents alteration.

A better question to ask is: do you actually have to display the actual URL itself? Can you get away with just a page/location name, just the server.host.domain prefix or some other condensed representation of the url? I don't think anyone whats to try to read a long url on a mobile's restricted screen.

If you do need to display the entire url then I think that a detail view is the way to go.

TechZen
I probably should have mentioned that the UI is a chat view. Messages will be shown in the table view, and if one of the users types or pastes a URL into the chat, it should be accessible. I don't want to alter the way the URL was written or pasted, as someone may base their message around that URL - something like: "Hey, check out this page: *link*" etc.
Jasarien
I tried something similar displaying urls and things got hairy in a hurry. Unless your talking about TinyUrls and the like they get unreadably long very quickly and they make the interface look like crap. Even for a chat I would recommend a detail view with just a truncated or symbolic representation in the main view.
TechZen