【问题标题】:UILabel height not changingUILabel 高度不变
【发布时间】:2014-07-05 05:13:47
【问题描述】:

我正在尝试更改自定义单元格中定义的两个标签高度,但多次失败。标签的高度应该随着填充的文本长度而变化。我已经成功实现了heightForRowAtIndexPAth中的单元格高度,但标签高度没有根据单元格高度进行调整。它要么截断文本,要么缩小字体。 SetNumberOfLines 归零似乎没有效果。 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;

【问题讨论】:

    标签: ios uitableview uilabel frame


    【解决方案1】:
    -(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;
    }
    

    【讨论】:

    • 我已经在自定义类中初始化了我的标签。 cell=nil 语句对我不起作用
    【解决方案2】:

    我通过在故事板上设置constraints 来修复它。就我而言,我计算了单元格高度,但我的标签高度没有相应调整。我按照以下步骤操作。

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-21
      • 1970-01-01
      相关资源
      最近更新 更多