0

I'm resizing a table's cell and a UILabel. The label seems to be resized, but remains on one line. How to deal with this?!

The label is set from the storyboard as:

Line Breaks: Word Wrap;
Autoshrink: Fixed Font Size;
Lines: 0

CGRect textRect = [cell.sampleLabel.text boundingRectWithSize:boundingSize options:NSStringDrawingUsesLineFragmentOrigin attributes: @{NSFontAttributeName:cell.sampleLabel.font} context:nil];

NSLog(@"textRect height: %f", textRect.size.height);
cell.sampleLabel.frame = textRect;
NSLog(@"label height: %f", cell.sampleLabel.frame.size.height);

The NSLog:

2013-12-20 11:04:41.623 DevCloud[16613:70b] textRect height: 223.091019
2013-12-20 11:04:41.624 DevCloud[16613:70b] label height: 224.000000
2013-12-20 11:04:41.626 DevCloud[16613:70b] textRect height: 223.091019
2013-12-20 11:04:41.627 DevCloud[16613:70b] label height: 224.000000
Neeku
  • 3,646
  • 8
  • 33
  • 43
Aleksandrenko
  • 2,987
  • 6
  • 28
  • 34

4 Answers4

1
#define FONT_SIZE 14.0f
#define CELL_CONTENT_WIDTH 280.0f
#define CELL_CONTENT_MARGIN 40.0f  




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

    {          
    NSString *text = @"your string";

    CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN ), 2000.0f);

    CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];

    CGFloat height = MAX(size.height, 20.0f);
    cellheight=height + (CELL_CONTENT_MARGIN * 2);
    return height + (CELL_CONTENT_MARGIN * 2);
    }


    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    static NSString *CellIdentifier = @"Cell";
    UILabel *label = nil;

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    for (UIView *view in cell.contentView.subviews) {
        [view removeFromSuperview];
    }

    NSString *text = @"your string";

    CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN ), 2000.0f);

    CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];
    commentscontentView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 290, MAX(size.height, 44.0f))];
    commentscontentView.tag=111;
    [cell.contentView addSubview:commentscontentView];


    label = [[UILabel alloc] initWithFrame:CGRectMake(20, 40, 220, 20)];
    [label setLineBreakMode:NSLineBreakByWordWrapping];
    [label setMinimumFontSize:FONT_SIZE];
    label.backgroundColor=[UIColor clearColor];
    [label setNumberOfLines:0];
    [label setFont:[UIFont fontWithName:@"OpenSans-Light" size:FONT_SIZE]];
    [label setTag:1];
    [commentscontentView addSubview:label];


    if (!label)
        label = (UILabel*)[cell viewWithTag:1];

    [label setText:text];


    [label setFrame:CGRectMake(20, CELL_CONTENT_MARGIN, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN), MAX(size.height, 44.0f))];
    label.textColor=[UIColor blackColor];
    [label setBackgroundColor:[UIColor clearColor]];


    return cell;
    }
Charan Giri
  • 1,097
  • 1
  • 9
  • 15
  • use this if want to increase height of label dynamically.. in cell configure we are dynamically increasing height of label and in height for row we are increasing cell height dynamically. – Charan Giri Dec 20 '13 at 09:28
1

try this one

cell.lbl_view2_disc1.numberOfLines=1000;
CGSize maximumLabelSize = CGSizeMake(260, FLT_MAX);
CGSize expectedLabelSize = [cell.lbl_view2_disc1.text sizeWithFont:calibri constrainedToSize:maximumLabelSize lineBreakMode:NSLineBreakByCharWrapping];

if your UIlabel width fix then set fix width in maximumLabelSize

CGSize maximumLabelSize = CGSizeMake(260, FLT_MAX);

if your UIlabel height fix then set fix height of UIlabel.frame.size.height in maximumLabelSize

CGSize maximumLabelSize = CGSizeMake(FLT_MAX,cell.lbl_view2_disc1.frame.size.height);

in my code working

nitin kachhadiya
  • 959
  • 2
  • 9
  • 21
0

Try

cell.sampleLabel.numberOfLines = 0
Wilmar
  • 1,451
  • 13
  • 17
0

Try

cell.sampleLabel.numberOfLines = 0
[cell.sampleLabel sizeToFit];
βhargavḯ
  • 9,786
  • 1
  • 37
  • 59