0

I am trying to get a list of comments from database. The comments might be even up to 100lines. The problem is that i can't get it to break line. I've used

comment.adjustFontSizeToFitWidth = NO;
comment.numberOfLines = 0;
comment.lineBreakMode = UILineBreakModeCharacterWrap

Curently the test comment is:

looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong

but it ends at middle, no "..." and no text wrap. How to fix this?

Btw. there are lots of cells like this.

M V
  • 83
  • 1
  • 10

3 Answers3

0
#define FONT_SIZE 11.0f
#define CELL_CONTENT_WIDTH 157.0f
#define CELL_CONTENT_MARGIN 10.0f

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
        NSString *text = YorCommentString;// or any String that u want.
        CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
        CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
        CGFloat height = MAX(size.height, 44.0f); // set as u want 
        return height + (CELL_CONTENT_MARGIN * 2); // set as u want

}

In above code no need to set numberOfLines for UILabel.

iPatel
  • 46,010
  • 16
  • 115
  • 137
  • comment is not showed... – M V Mar 12 '13 at 11:39
  • 1
    This is in a table view cell. The lable itself may be adjusted by size to fit and the table cell view might. But the table will always only provide that much space as is given in its rowHeight property or -if- as returned by tableView:heightForRowAtIndexPath:. So he does not come around acutally calculating the required size and derive from that the size of the cell and provide exatyl that by implmenting tableView:heightForRowAtIndexPath: – Hermann Klecker Mar 12 '13 at 11:51
0

Do this thing in your tableview delegate :

- (CGFloat)tableView:(UITableView *)tableView1 heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
        CGSize maximumSize = CGSizeMake(480.0,1000.0); // Put appropriate required height and width.
        NSString *dateString = [NSString stringWithFormat:@"%@",yourString];
        UIFont *dateFont = [UIFont fontWithName:@"Helvetica" size:14];
        CGSize dateStringSize = [dateString sizeWithFont:dateFont 
                                       constrainedToSize:maximumSize 
                                           lineBreakMode:UILineBreakModeWordWrap];
        return dateStringSize.height;
}

This code will set appropriate height for your cell. Then in your cellForRowAtIndexPath function. keep this code :

comment.adjustFontSizeToFitWidth = NO;
comment.numberOfLines = 0;
comment.lineBreakMode = UILineBreakModeCharacterWrap
Rushi
  • 4,553
  • 4
  • 33
  • 46
0

You will have to impement tableView:heightForRowAtIndexPath: See http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewDelegate_Protocol/Reference/Reference.html for details.

Within that method you will have to calculate the height that each of the cells requries based on the height that the lable requires to display the text. In the UIExtenstions of NSString you will find the helper methods for exactly that calculation. http://developer.apple.com/library/ios/#documentation/uikit/reference/NSString_UIKit_Additions/Reference/Reference.html

So is full with examples on how to use – sizeWithFont:constrainedToSize:lineBreakMode:.

Then you can either layout the cell items in cellForRowAtIndexPath where you need to use sizeWithFont:... again for the calculation of the size of the text. Or, if you want a neat solultion, better subclass UITableViewCell and overwrite its layoutSubviews method and do the layout there.

Hermann Klecker
  • 14,039
  • 5
  • 48
  • 71