gpt4 book ai didi

ios - 具有动态高度的 UILabel 到 UITableviewcell

转载 作者:行者123 更新时间:2023-11-28 22:15:45 24 4
gpt4 key购买 nike

我正在开发一个需要将 UILabel 显示到 UITableviewCell 中的应用程序。我还需要根据文本大小调整 UILabel 的大小。我正在使用以下代码获取文本大小的 contentsize

CGRect rect = [as boundingRectWithSize:CGSizeMake(220.0, 2000.0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: font} context:nil];

对于 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];

它设置了错误的框架。请查找随附的屏幕截图。 enter image description here

最佳答案

    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];

关于ios - 具有动态高度的 UILabel 到 UITableviewcell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21751521/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com