Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display multiple lines in a tableView cell

I have a somewhat long string that gets truncated when displayed in a tablView cell. I did this to make the table view wider:

tableView.rowHeight = 100;

How can I make the font smaller as well as wrap the text in the table view cell?

like image 259
node ninja Avatar asked Sep 23 '10 00:09

node ninja


1 Answers

In tableView:cellForRowAtIndexPath: you can set a few properties on the textLabel (or descriptionLabel, depending on what style of cell you're using) to do this. Set font to change the font, linkBreakMode to make it word-wrap, and numberOfLines to set how many lines max (after which point it truncates. You can set that to 0 for no max.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell* aCell = [tableView dequeueReusableCellWithIdentifier:kMyCellID];
    if( aCell == nil ) {
        aCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kMyCellID] autorelease];

        aCell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:10.0];
        aCell.textLabel.lineBreakMode = NSLineBreakByWordWrapping; // Pre-iOS6 use UILineBreakModeWordWrap
        aCell.textLabel.numberOfLines = 2;  // 0 means no max.
    }

    // ... Your other cell setup stuff here

    return aCell;
}
like image 187
zpasternack Avatar answered Oct 17 '22 02:10

zpasternack