views:

121

answers:

3

I want to add a custom table to my iPhone app, that should look and work like the one used in the facebook app showing the notifications. It should contain rows with links in it. The text should be black, while the tap-able parts should appear blue.

As a already figured out, labels only have one font, color and so on and you can't mix styles within it, I would have to use different components. The tab-able text parts maybe UIButtons with custom style without any border.

I tried different approches to build the custom cell. I used Interface Builder, to layout components, but since the different parts have variable lengths they overlaid each other. I also tried to add instances of UIButton and UILabel as a subview to the cell's contentView as descibed in Apple's tutorial. The problem with this is that: The position and size of the components is fix and set when they get created using "initWithFrame:...".

In a nutshell, I want to add the labels and buttons without a fix position or size, the buttons clickable and the cell height should automatically set according to the content.

Here is a screenshot of the Facebook's App notifications view: faceboo app notifications

A: 

In your facebook example it seems that this is only one line with a linebreak.

To allow linebreaks, just add this to your cellForRowAtIndexPath


// load cell description; set nrOfLines to 0 to allow linebreaks
cell.detailTextLabel.numberOfLines = 0;

With that you should be able to deal with fixed positions an dynamic content.

I think that's not the solution. I have to use more than one UILabel, because I want different styles, like color and size. Beside this, the clickable links have to be UIButtons.
Tim Büthe
+2  A: 

Option 1: Use a UIWebview to display the text and format it as HTML. For the links, use a custom URL that sends back to your app which responds by displaying the appropriate view.

Option 2: Use the NSString UIKit Additions to directly manage either the size of the labels or to draw the text into images which can function as buttons. In either case, you will use the addition's methods to calculate the display size of the string in pixels and then adjust the size of the individual display view (label, button, image etc) as needed before the tableview's delegate returns the cell to the tableview.

Either options takes a lot of careful coding. It will be nice when the iPhoneOS supports attributed strings.

Edit:

It looks like Three20 has a styled text view. It also looks like you can put in tables and that it supports links. I haven't used it but it may do what you want.

TechZen
Thanks for your answer, but I can't believe that this is such a hard task. Both options seem to me like a hack.
Tim Büthe
They are hacks because the iPhoneOS does not yet support formatted text. For any interface element, you get one font and one color. If you want mixed fonts/colors you have to hack.
TechZen
As an aside, I think the Facebook interface a poor one. It may look pretty but if it formats with two links one immediately below the other, its almost impossible to hit the the right link with relatively pudgy human fingers. I think its uses spend a lot of time hitting the wrong link and having to backtrack. You should test this out before emulating it.
TechZen
@TecgZen: I find your links really interesting. Thanks a lot
vodkhang
I had no time to try your Three20 link, but it looks promising. Thanks a lot.
Tim Büthe
+1  A: 

You would think this would be easy, but it's actually pretty damn hard. UIKit isn't set up for this. The easiest way is to use the Three20 library -- at least read the code, so you'll get an understanding of what it takes.

There might be something in OS 4 that makes it easier. If you find out, please post it!

Vineel Shah