简体   繁体   中英

How to extend uilabel height as per content?

I want to change the uilabel height as per content and display it in a uitableview cell, there is a custom cell and cell is expand as per uilabel height When button is pressed then and then cell height is expand as per the uilabel height

Thank you in Advance :-)

//Calculate the size based on the font and linebreak mode of your label

CGSize maxLabelSize = CGSizeMake(300,9999);

CGSize expectedLabelSize = [myString sizeWithFont:myLabel.font 
                        constrainedToSize:maxLabelSize 
                        lineBreakMode:myLabel.lineBreakMode]; 

//adjust the label the the new height.
CGRect newFrame = myLabel.frame;
newFrame.size.height = expectedLabelSize.height;
myLabel.frame = newFrame;

Try this

    [label sizeToFit];

Note that label will increase it's height keeping it's width the same.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *text = displayText;

//CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize constraint = CGSizeMake(tableView.frame.size.width - 30, 20000.0f);

CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:15.0f] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];

CGFloat height = MAX(size.height, 44.0f);

return height + 30;

}

计算标签文字的大小,然后将坐标设置为

CGSize labelWidth=[_label.text sizeWithFont:[UIFont fontWithName:@"Verdana" size:12] constrainedToSize:CGSizeMake(200, 15) lineBreakMode:UILineBreakModeTailTruncation];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM