[英]UILabel with dynamic height into UITableviewcell
I am developing an app which required to display UILabel
into UITableviewCell
. 我正在开发一个应用程序,需要将
UILabel
显示到UITableviewCell
。 I also need to resize UILabel
as per text size. 我还需要根据文本大小调整
UILabel
大小。 I am using following code for get contentsize
of text size. 我正在使用以下代码来获取文本大小的
contentsize
。
CGRect rect = [as boundingRectWithSize:CGSizeMake(220.0, 2000.0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: font} context:nil];
For update frame of UILabel
I use following code. 对于
UILabel
更新框架,我使用以下代码。
rect.origin.x = cell.lblDescription.frame.origin.x;
rect.origin.y = cell.lblDescription.frame.origin.y;
rect.size.width = cell.lblDescription.frame.size.width;
[cell.lblDescription setFrame:rect];
It set wrong frame. 它设置了错误的框架。 Please find attached screenshot.
请查找附带的截图。
You have to check the height of the label in - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
UITableViewDelegate Method. 你必须检查标签的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
UITableViewDelegate方法。
CGSize maximumLabelSize = CGSizeMake(your_label_width, FLT_MAX);
CGSize expectedLabelSize = [label_text sizeWithFont:[UIFont fontWithName:@"your_font" size:your_font_size] constrainedToSize:maximumLabelSize lineBreakMode:NSLineBreakByWordWrapping];
then set the cell height as expectedLabelSize.height
. 然后将单元格高度设置为
expectedLabelSize.height
。
also do the same thing in your custom cell. 在自定义单元格中也做同样的事情。
NSString *text = [NSString stringWithFormat:@"%@",[[arr_cart objectAtIndex:indexPath.row] objectForKey:@"name"]];
UIFont *font = [UIFont fontWithName:@"ArialMT" size:12];
CGSize size = [(text ? text : @"") sizeWithFont:font constrainedToSize:CGSizeMake(200, 9999) lineBreakMode:NSLineBreakByWordWrapping];
UILabel *lbl_desc=[[UILabel alloc]init];
lbl_desc.numberOfLines = 0;
lbl_desc.frame=CGRectMake(70,18, size.width, size.height);
lbl_desc.lineBreakMode = NSLineBreakByWordWrapping;
lbl_desc.text = (text ? text : @"");
lbl_desc.font = font;
lbl_desc.backgroundColor=[UIColor clearColor];
lbl_desc.textColor = [UIColor darkTextColor];
[cell.contentView addSubview:lbl_desc];
[lbl_desc release];
Use following code 使用以下代码
[self setDynamicHeightOfLabel:lblName withLblWidth:97 andFontSize:13];
In above method you just need to pass your UILabel
name with specific width and fontSize of label. 在上面的方法中,您只需要传递具有特定宽度和标签的fontSize的
UILabel
名称。
-(void) setDynamicHeightOfLabel:(UILabel *) myLabel withLblWidth:(CGFloat) width andFontSize:(int) fontSize
{
CGSize myLabelSize = CGSizeMake(width, FLT_MAX);
CGSize expecteingmyLabelSize = [myLabel.text sizeWithFont:myLabel.font constrainedToSize:myLabelSize lineBreakMode:myLabel.lineBreakMode];
CGRect lblFrame = myLabel.frame;
lblFrame.size.height = expecteingmyLabelSize.height;
myLabel.frame = lblFrame;
int addressLine = myLabel.frame.size.height/fontSize;
myLabel.numberOfLines = addressLine;
}
Using above code you can set dynamic height of UILabel
. 使用上面的代码,您可以设置
UILabel
动态高度。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.