UILabel does not change height to setFrame

I am trying to change the height UILabeldepending on how much text is in the label.

I can calculate the size needed for the label, but when I try to set the frame UILabel, it just doesn't change.

Below is my code. Even if I replace size.height in the last line with about 500, the frame size UILabelwill not change

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    static NSString *simpleTableIdentifier = @"GameItemCell";
    GameItemCell *cell = (GameItemCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"GameItemCell" owner:self options:nil];
    cell = [nib objectAtIndex:0];

    GameItem* item = [_hunt.gameItems objectAtIndex: indexPath.row];


    cell.itemHeaderLabel.text = [NSString stringWithFormat:@"#%d - (%d pts)", indexPath.row+1, item.itemPoints];

    UILabel* textLabel = cell.itemTextLabel;

    textLabel.text = item.itemText;
    textLabel.lineBreakMode = NSLineBreakByWordWrapping;


    CGRect frame = cell.itemTextLabel.frame;
    CGSize textSize = { frame.size.width, 20000.0f };
    CGSize sizeOneLine = [@"one line" sizeWithFont:cell.itemTextLabel.font constrainedToSize:textSize lineBreakMode:NSLineBreakByWordWrapping];
    CGSize cellTextSize = [item.itemText sizeWithFont:cell.itemTextLabel.font constrainedToSize:textSize lineBreakMode:NSLineBreakByWordWrapping];
    CGSize sizeOneLineSpacing = CGSizeMake(sizeOneLine.width, sizeOneLine.height + 3);
    NSInteger lines = cellTextSize.height / sizeOneLine.height;
    CGSize size = CGSizeMake(frame.size.width, lines * sizeOneLineSpacing.height);

    textLabel.frame = CGRectMake(frame.origin.x, frame.origin.y, size.width, size.height);



    return cell;
}
+5
source share
8 answers

You must set the frame of your label in GameItemCell inside -(void)layoutSubviews

+7
source

Instead of doing all this hard work, try:

textLabel.numberOfLines = 0;
textLabel.text = textString;
[textLabel sizeToFit];

sizeToFit , . Height sizeToFit.

+2

- UITableViewCell:

// call this method on your cell, during cellForRowAtIndexPath
// provide your resizing info (frame, height, whatever)

- (void) updateLabelFrame:(CGRect)newLabelFrame {
    self.resizedLabelFrame = newLabelFrame;
    [self setNeedsLayout];
}

// the actual resize happens here when UIKit gets around to it

- (void) layoutSubviews {
    [super layoutSubviews];
    self.myLabel.frame = self.resizedLabelFrame;
}
+2

numberOfLines .

:

textLabel.numberOfLines = 0;
+1

numberOfLines

, .

: 1. , 0.

UILabel

, ,

textLabel.numberOfLines = lines;
0
CGRect labelFrame = myLabel.frame;

labelFrame.size = [myLabel.text sizeWithFont:myLabel.font             constrainedToSize:CGSizeMake(myLabel.frame.size.width, CGFLOAT_MAX)
                                lineBreakMode:myLabel.lineBreakMode];

cell.textLabel.frame = labelFrame;
0
source

Yes, for multiple lines use textLabel.numberOfLines = 0; in cellForRowAtIndexPath

But you also need to change the cell height:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Calculate new cell height too as you are doing in cellForRowAtIndexPath 
    return YOUR_SIZE;;
}
0
source

Try the following:

        NSString *text1 = shareObjC.commentText;
        CGSize constraint1 = CGSizeMake(280 - (size.width + 5), 2000);

        CGSize size1 = [text1 sizeWithFont:[UIFont systemFontOfSize:12] constrainedToSize:constraint1 lineBreakMode:UILineBreakModeWordWrap];

        UILabel *lblComment = [[[UILabel alloc] initWithFrame:CGRectMake(posx,posy,size1.width,size1.height)] autorelease];

        lblComment.lineBreakMode = UILineBreakModeWordWrap;
        lblComment.numberOfLines = size1.height/15;
        [lblComment setFont:[UIFont systemFontOfSize:12]];
        lblComment.text = text1;
        [cell.viewLikeComment addSubview:lblComment];
0
source

All Articles