UILabel高度不改变



我试图改变自定义单元格中定义的两个标签高度,但多次失败。标签的高度应该随文本的长度而变化。我已经成功地实现了heightForRowAtIndexPAth的单元格高度,但标签高度没有根据单元格高度进行调整。它截断文本或缩小字体。SetNumberOfLines到0似乎没有影响。ContentTextViewTransView是我的两个uiLabels

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Customcell";
    CustomeCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier   forIndexPath:indexPath];
    NSString *text = [_detailItem objectAtIndex:[indexPath row]];
    NSString *text2 = [_detailItem2 objectAtIndex:[indexPath row]];

    CGSize maxSize = CGSizeMake(296.f, FLT_MAX);
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text];
    NSMutableAttributedString *attributedString2 = [[NSMutableAttributedString alloc] initWithString:text2];
    CGRect rect = [attributedString boundingRectWithSize:maxSize
                                 options:NSStringDrawingUsesLineFragmentOrigin
                                 context:nil];
    CGRect rect2 = [attributedString2 boundingRectWithSize:maxSize
                                                          options:NSStringDrawingUsesLineFragmentOrigin
                                                 context:nil];

    CGSize maximumLabelSize = CGSizeMake(500,CGFLOAT_MAX);
    CGSize requiredSize = [cell.contentTextView sizeThatFits:maximumLabelSize];
    CGSize requiredSize2 = [cell.transView sizeThatFits:maximumLabelSize];
    CGRect labelFrame = cell.contentTextView.frame;
    CGRect labelFrame2 = cell.transView.frame;
    labelFrame.size.height = requiredSize.height;
    labelFrame2.size.height=requiredSize2.height;
    cell.contentTextView.frame = labelFrame;
    cell.transView.frame=labelFrame2;
    [attributedString addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, [text length])];
    [attributedString2 addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, [text2 length])];
    [cell.contentTextView setLineBreakMode:NSLineBreakByWordWrapping];
    [cell.contentTextView setMinimumScaleFactor:FONT_SIZE];
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    [style setLineSpacing:2];
    [style setAlignment:(NSTextAlignment)kCTRightTextAlignment];
    [cell.contentTextView setNumberOfLines:0];
    [ cell.transView setNumberOfLines:0];
    cell.selectionStyle = UITableViewCellSelectionStyleBlue;
    tableView.separatorColor = [UIColor colorWithRed:0.576 green:0.796 blue:0.008 alpha:1];
    cell.contentTextView.attributedText = attributedString;
    cell.transView.attributedText=attributedString2;
    return cell;
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellIdentifier = @"Cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell==nil) 
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        UILabel *lblText = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, 275, [self dynamicHeightAtIndexPath:indexPath])];
        lblText.backgroundColor = [UIColor clearColor];
        [lblText setTag:1];
        [cell.contentView addSubview:lblText];
     }
     UILabel *lbl = (UILabel *)[cell.contentView viewWithTag:1];
     lbl.numberOfLines=0;
     lbl.attributedText = @"label text";
     return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
     return [self dynamicHeightAtIndexPath:indexPath]+20;
}
-(CGFloat)dynamicHeightAtIndexPath:(NSIndexPath *)indexPath
{
    CGSize maximumSize = CGSizeMake(275, 9999);
    UIFont *myFont =[UIFont fontWithName:@"Halvetica" size:12]; //your font for label
    CGSize stringsize = [[self.array objectAtIndex:indexPath.row] sizeWithFont:myFont
                                                         constrainedToSize:maximumSize
                                                             lineBreakMode:NSLineBreakByWordWrapping];
    return stringsize.height;
}

我通过在故事板上设置constraints来修复它。在我的情况下,我已经计算了单元格高度,但我的标签高度没有相应地调整。我遵循以下步骤。

1-在故事板上,点击底部出现的"Pin"并设置约束(依次点击标签)。

2-在尺寸检查器中,将每个标签的固有尺寸更改为"占位符"。

3-确保在设置约束后修复所有警告,否则它将不起作用

最新更新