【每日技术点】13.12.13

1、UITextView在UITableViewCell 中自适应高度的问题

 1 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 2 {
 3    。。。。。。
 4 
 5 UITextView * contentView = [[UITextView alloc] initWithFrame:CGRectZero];
 6         contentView.textColor = [UIColor colorWithRed:80.0/255.0 green:132.0/255.0 blue:183.0/255.0 alpha:1.0];
 7         [contentView setFont:[UIFont systemFontOfSize:12.0f]];
 8         [contentView setBackgroundColor:[UIColor clearColor]];
 9         [contentView setEditable:NO];
10         contentView.contentInset = UIEdgeInsetsMake(15,-8,0,18);//这句很重要,因为textview中的text也是有自己的边界的。
11         [cellBgView addSubview:contentView];
12         
13         NSString *text = [contentArray objectAtIndex:[indexPath section]];
14         CGSize constraint = CGSizeMake(200, 20000.0f);
15         CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:12.0f] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];
16         [contentView setFrame:CGRectMake(10, 10, 280, MAX(size.height, 44.0f))];
17 
18  }
1 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
2 {
3     NSString *text = [contentArray objectAtIndex:[indexPath section]];
4     CGSize constraint = CGSizeMake(200, 20000.0f);
5     CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:12] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];
6     CGFloat height = MAX(size.height, 44.0f);
7     return height + (10 * 2);
8 }
原文地址:https://www.cnblogs.com/ymonke/p/3472990.html