Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

beachdog

macrumors member
Original poster
Aug 10, 2008
86
0
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?
 

Luke Redpath

macrumors 6502a
Nov 9, 2007
733
6
Colchester, UK
The first basic approach would be to create a UILabel for each individual text component needed and add these as subviews of the UITableViewCell, positioned appropriately.

You might find after doing this that your code is cleaner if you extract this into your own custom UITableViewCell subclass (especially if you intend to use it in more than one place).

The iPhone developer docs has a whole section on customizing table cells in the Table view programming guide.
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
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.
 

iphonejudy

macrumors 6502
Sep 12, 2008
301
1
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?

Have you got the solution?if,Please let me know
 

sujithkrishnan

macrumors 6502
May 9, 2008
265
0
Bangalore
Multiline text in UITableViewCell

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.
 

Attachments

  • Snapshot 2009-03-13 12-23-06.png
    Snapshot 2009-03-13 12-23-06.png
    24 KB · Views: 9,517

iphonejudy

macrumors 6502
Sep 12, 2008
301
1
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.

Ya suji.I want like this
 

North Bronson

macrumors 6502
Oct 31, 2007
395
1
San José
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 had this problem and I ended up using this:

Code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	CustomCell *selectedCustomCell = (CustomCell *)[tableView cellForRowAtIndexPath: indexPath];

	UIView *selectedContentView = [selectedCustomCell contentView];

	CGRect selectedBounds = selectedContentView.bounds;

	NSLog (@"Origin x: %@: selectedBounds.origin.x);

	NSLog (@"Origin y: %@: selectedBounds.origin.y);

	NSLog (@"Size width: %@: selectedBounds.size.width);

	NSLog (@"Size height: %@: selectedBounds.size.height);

//	More code
}

This should pop out the dimensions of any cell you touch. You can see that if you have an accessory (like a check-mark or a chevron) the size adjusts accordingly.
 

caveman_uk

Guest
Feb 17, 2003
2,390
1
Hitchin, Herts, UK
Use this method from NSString to get the height of a string
Code:
- (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(UILineBreakMode)lineBreakMode
None of the others (AFAIK) actually do wrapping except this one

Link
 

acjrocks

macrumors member
Mar 24, 2008
30
0
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.

Still have the source for this? I sure could use this. Still learning about custom table views and cells.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.