3

I have a tableivew with a bunch of cells and I am trying to get the uilabel to display more than 3 lines. I set the linebreakmode and the numberoflines appropriately, but it's still not displaying more than three lines. Any suggestions? The table cell automatically adjusts its height to accomodate the number of chars/lines, but the text shows three lines and then an ellipse (when you click the cell it goes to another view which shows the full text.

Below is the code that I have to create and display the UILabel:

   self.commentLabel = [self newLabelWithPrimaryColor:[UIColor blackColor] selectedColor:[UIColor whiteColor] fontSize:12.0 bold:YES];
    self.commentLabel.textAlignment = UITextAlignmentLeft; // default
    self.commentLabel.lineBreakMode = UILineBreakModeWordWrap;
    self.commentLabel.numberOfLines = 0; // no limit to the number of lines 
    [myContentView addSubview:self.commentLabel];
    [self.commentLabel release];

I would like the entire comment to be displayed in the table cell.

Krunal
  • 77,632
  • 48
  • 245
  • 261
  • Advice: prefixing methods with "new" (newLabelWithPrimaryColor) will generate compiler warnings on newer llvm clang compilers. It is a naming convention that hints that the value being returned is not autoreleased and that caller should call release. A better name would be labelWithPrimaryColor:. – Joe Jun 22 '11 at 17:08

2 Answers2

1

Seems like the rectangle of the label is too small to fit all the text...You must make the rectangle bigger in order for it to not display the ellipses and display the whole text

Daniel
  • 22,363
  • 9
  • 64
  • 71
  • Yes - I was resizing the tablecell, but not the UILabel. I went to the method that was creating the Label and created a math function to set the height based on the number of characters in the comment. Thanks Daniel. –  Jul 14 '09 at 17:31
0

With autolayout design concept, do not set height constraints for UILabel and set no. of lines as 0.

Autolayout automatically take care of dynamic height of label according to text of label. If label has single line text then it will occupy single line space only. And if label has more than one line then it will resize label according to text size and number of lines required to display text.

  • Assign and implement tableview dataSource and delegate
  • Assign UITableViewAutomaticDimension to rowHeight & estimatedRowHeight
  • Implement delegate/dataSource methods (i.e. heightForRowAt and return a value UITableViewAutomaticDimension to it)

-

Objective C:

// in ViewController.h
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>

  @property IBOutlet UITableView * table;

@end

// in ViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];
    self.table.dataSource = self;
    self.table.delegate = self;

    self.table.rowHeight = UITableViewAutomaticDimension;
    self.table.estimatedRowHeight = UITableViewAutomaticDimension;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return UITableViewAutomaticDimension;
}

Look at this answer: Adjust UILabel Dynamically Within UITableViewCell?

Krunal
  • 77,632
  • 48
  • 245
  • 261