宽容他人,放过自己。

cell中label高度自适应

Posted on By anchoriteFili

tableView内每行高度的自适应

#pragma mark cell高度的自适应
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//    计算高度
    NSString *key = [[self.contactDic allKeys] objectAtIndex:indexPath.section];
    NSArray *arr = [self.contactDic objectForKey:key];
    Person *p = [arr objectAtIndex:indexPath.row];
    NSString *hobby = p.hobby;
    
    CGRect rect = [hobby boundingRectWithSize:CGSizeMake(200, 1000)
                                      options:NSStringDrawingUsesLineFragmentOrigin
                                   attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:17]}
                                      context:nil];
    NSLog(@"rect = %@",NSStringFromCGRect(rect));
    
    return 110 + rect.size.height;
}

cell内label的自适应

CGRect frame = self.hobbyLabel.frame;
    CGRect rect = [p.hobby boundingRectWithSize:CGSizeMake(200, 1000)
                                        options:NSStringDrawingUsesLineFragmentOrigin
                                     attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:17]}
                                        context:nil];
    frame.size.height = rect.size.height;
    self.hobbyLabel.frame = frame;