tableView_cell中label自适应高度

首先让tableView_cell自适应高度

 1 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
 2
 3     Commodity *com = [self.comArr objectAtIndex:indexPath.row];
 4 //    跟据label中字数的多少,字数的大小和label自身的宽度,来确定其所占的高度。
 5     CGRect rect = [com.description1 boundingRectWithSize:CGSizeMake(167, 1000)
 6                                                  options:NSStringDrawingUsesLineFragmentOrigin
 7                                               attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:14]}
 8                                                  context:nil];
 9 //    cell中控件儿的固定高度加上label的变化的高度,确定cell的高度
10     return 80 + rect.size.height;
11 }

cell和label中的自适应高度中的数据一定要和实际数据相同,字体大小,label的宽度都要相等,这样才能准确显示

如果使用storyboard,要使cell和其中的控件儿自适应高度,就必须将cell属性栏第一栏中的Use Auto Layout点成空

在cell类中,在重写实例变量方法中写label自适应高度方法

 1 - (void)setCom:(Commodity *)com {
 2
 3     self.comName.text = com.title;
 4     self.comDescription.text = com.description1;
 5
 6 //    在cell类中,在重写的某一实例变量中使label自适应高度
 7     CGRect frame = self.comDescription.frame;
 8     CGRect rect = [self.comDescription.text boundingRectWithSize:CGSizeMake(167, 1000)
 9                                                          options:NSStringDrawingUsesLineFragmentOrigin
10                                                       attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:14]}
11                                                          context:nil];
12     frame.size.height = rect.size.height;
13     self.comDescription.frame = frame;
14 }

cell的自适应高度完成

时间: 2024-12-27 00:48:51

tableView_cell中label自适应高度的相关文章

TableView中Label自适应高度

//Xcode6.3以后label自适应需要添加两个属性 _tableView.rowHeight = UITableViewAutomaticDimension; //给予预计行高 _tableView.estimatedRowHeight = 44;

iOS Label 自适应高度

推荐第二个 测试一,只改变numberOfLines属性,label的高度不会自适应(会有text中的一部分内容称为......) NSString *str = @"jgreijgirjeirgjierjgiu4t9eumctuv5 vtmnvghvmc5v5tgh58tc857y"; UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(50, 50, 100, 100)]; label.font = [UIFont s

iOS textFiledView,label自适应高度

CGSize constraintSize; constraintSize.width = 320; constraintSize.height = MAXFLOAT; CGSize sizeFrame =[infoTextView.text sizeWithFont:infoTextView.font = [UIFont systemFontOfSize:12.0] constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWord

Label自适应高度的用法及设置倒角

UILabel *label = [[UILabel alloc] init]; //根据内容动态计算label的高度 label.text = @"Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions"; //1.获取label当前使用的字体 UIFont *labelFo

Label自适应高度

每次都逼我翻代码   这次干脆写博客里面算了 哈哈哈 CGSize maxSize = CGSizeMake(ScreenWith-30,NSIntegerMax); CGSize labelsize = [addressContentLabel.text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NS

TabelViewCell自适应高度

1.首先,设置 cell 中显示文本的容器(这里假设是 Label)的一些属性,如下: [_LabelsetNumberOfLines:0];//这个是设置 label 内文本的行数,0 代表自适应 [_LabelsetLineBreakMode:NSLineBreakByWordWrapping];//断行模式 [_LabelsetFont:[UIFontsystemFontOfSize:16.0]];//字体大小 2.在 Cell 的 - (void)layoutSubviews 方法中,重

IOS UILabel 根据内容自适应高度

iOS Label 自适应高度  适配iOS7以后的版本 更多 self.contentLabelView = [[UILabel alloc] init]; self.contentLabelView.font = SYS_FONT(15); self.contentLabelView.lineBreakMode =NSLineBreakByTruncatingTail ; self.contentLabelView.textColor =  [UIColor colorWithHexStri

怎样让自定义Cell的图片和文本自适应高度

Let's do it! 首先创建一个Model类 包括一个图片名称属性 还有文字内容属性 #import <Foundation/Foundation.h> @interface Model : NSObject @property (nonatomic, copy) NSString *imageName; @property (nonatomic, copy) NSString *info; @end 创建一个继承于UITableViewCell的MyTableViewCell,把模型作

xib中实现cell高度自适应

iOS8之后的tableview中cell可以设置成自适应高度: 这个新特性,意味着View被Autolayout调整frame后,会自动拉伸和收缩SupView. 具体到Cell,要求cell.contentView的四条边都与内部元素有约束关系. 在TableViewController里 - (void)viewDidLoad { [super viewDidLoad]; //添加这两行代码 self.tableView.estimatedRowHeight = 44.0f; self.t