Multiple text lines in a UITableView

macrumors member

I've done some basic programming of UITableViews, but now I have a need to create a table view with cells that contain multiple lines of text, preferably where each line may have a different Font size. I need to create something that looks like the table view in the "Inbox" view of the mail application. Can someone give me some pointers on this?

macrumors 68030

If each line in each cell can have a different font then you need a separate Label for each line. If each cell has multiple lines but each cell has only a single font then you can use a single Label in each cell. You don't actually say if all the cells have the same number of lines or if they vary. If they are all the same then just look at the docs and examples. There are several that show how to have multiple lines with different font characteristics.

If you have a variable number of lines you will need to use variable row heights. There is a tableview delegate callback, heightForRowAtIndexPath, that allows you to tell the table the row height for each cell. You will need to calculate the rowheight using NSString:sizeWithFont:constrainedToSize:lineBreakMode:, which isn't as much fun as it sounds. Unfortunately I couldn't figure out a way to determine the width of the cell for certain. What I do is get the width of the screen (320 or 480 depending on orientation) and subtract a factor that I determined emperically from that width. I have a label in my cell so my width won't work for you. Try different widths until you get one that gives you the correct height for your text.

macrumors 6502

I've done some basic programming of UITableViews, but now I have a need to create a table view with cells that contain multiple lines of text, preferably where each line may have a different Font size. I need to create something that looks like the table view in the "Inbox" view of the mail application. Can someone give me some pointers on this?

macrumors 6502

Are u trying to do something like in the given attached file.If so then create custom view,in draw rect of it draw string.And cell height should be the height of string that u want to display.Custom view should be added as subView to cell.contentView.If u are trying to do something like this,then i can give u code.

Attached Files:

macrumors 6502

Are u trying to do something like in the given attached file.If so then create custom view,in draw rect of it draw string.And cell height should be the height of string that u want to display.Custom view should be added as subView to cell.contentView.If u are trying to do something like this,then i can give u code.

macrumors 6502

Unfortunately I couldn't figure out a way to determine the width of the cell for certain. What I do is get the width of the screen (320 or 480 depending on orientation) and subtract a factor that I determined emperically from that width.

macrumors member

Are u trying to do something like in the given attached file.If so then create custom view,in draw rect of it draw string.And cell height should be the height of string that u want to display.Custom view should be added as subView to cell.contentView.If u are trying to do something like this,then i can give u code.

Click to expand...

Still have the source for this? I sure could use this. Still learning about custom table views and cells.

MacRumors attracts a broad audience
of both consumers and professionals interested in
the latest technologies and products. We also boast an active community focused on
purchasing decisions and technical aspects of the iPhone, iPod, iPad, and Mac platforms.