5

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?

node ninja
  • 31,796
  • 59
  • 166
  • 254

3 Answers3

14

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;
}
zpasternack
  • 17,838
  • 2
  • 63
  • 81
  • 1
    As far as I know the only thing deprecated in iOS6 is `UILineBreakModeWordWrap`. `NSLineBreakByWordWrapping` is the iOS6 equivalent. Updated my answer. – zpasternack Sep 27 '12 at 21:16
2
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell* Cell = [tableView dequeueReusableCellWithIdentifier:kMyCellID];
    if( Cell == nil ) {
        Cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kMyCellID] autorelease];

       Cell.textLabel.font = [UIFont fontWithName:@"TimesNewRoman" size:10.0];
       Cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
       Cell.textLabel.numberOfLines = 2;  // 0 means no max.
    }

  // your code here

    return Cell;
}
j0k
  • 22,600
  • 28
  • 79
  • 90
DC9999
  • 29
  • 2
0

You should subclass UITableViewCell and make it have a UITextView instead of a UITextField. Then assign your subclass to your UITableView.

Altealice
  • 3,572
  • 3
  • 21
  • 41