根据文本行自动计算标签的高度

时间:2015-11-18 03:59:57

标签: ios objective-c uitableview autolayout masonry

到目前为止,我一直致力于计算自定义单元格高度。我需要在获取数据后自动调整单元格大小。

self.countLabelself.descriptionLabel只占用一行文字,另一方面,self.detailLabel可能包含多行。所以下面的代码就是我如何设置约束。

enter image description here

CustomCell.m

- (void)initView
{
    self.countLabel = [UILabel new];
    self.countLabel.text = @"AAAAAA";
    self.countLabel.font = [UIFont fontWithPixel:22];
    self.countLabel.numberOfLines = 1;
    [self.contentView addSubview:self.countLabel];

    self.detailLabel = [UILabel new];
    self.detailLabel.text = @"Number of people: ";
    self.detailLabel.font = [UIFont fontWithPixel:22];
    self.detailLabel.textColor = [UIColor qu_grayColor];
    [self.contentView addSubview:self.detailLabel];

    self.descriptionLabel = [UILabel new];
    self.descriptionLabel.numberOfLines = 1;
    self.descriptionLabel.text = @"ABCDEFGHIJKLM";
    self.descriptionLabel.font = [UIFont fontWithPixel:26];
    [self.contentView addSubview:self.descriptionLabel];

    [self.countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(20);
        make.top.equalTo(self.contentView).offset(5);
        make.right.equalTo(self.contentView).offset(-10);
    }];

    [self.detailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(20);
        make.top.equalTo(self.countLabel.mas_bottom);
        make.bottom.equalTo(self.descriptionLabel.mas_top);
        make.right.equalTo(self.contentView).offset(-10);
    }];

    [self.descriptionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(10);
        make.right.equalTo(self.contentView).offset(-10);
        make.bottom.equalTo(self.contentView).offset(-15);

    }];
}

5 个答案:

答案 0 :(得分:0)

self.detailLabel.text = @"Number of peoples: ";
self.detailLabel.font = [UIFont fontWithPixel:22];
...
[self setLabelHeight:self.detailLabel];


- (void)setLabelHeight:(UILabel *)label {
    CGFloat fixedWidth = label.frame.size.width;
    CGSize newSize = [label sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)];
    CGRect newFrame = label.frame;
    newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height);
    label.frame = newFrame;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    //if indexPath.row == detailsLabelRow
    //return self.detailLabel.height
    //else 
    //return 1 line height
}

答案 1 :(得分:0)

UILabel中有多行,你必须先将行数设置为零。

self.countLabel.numberOfLines = 0;

然后设置文本调用方法后。

[self.countLabel sizeToFit];

或者您可以创建类别类并在该类中添加以下方法。

-(void)setText:(NSString *)text {
    [super setText:text];
    [self sizeToFit];
}

如果您不需要随处调用它将会很有帮助。

答案 2 :(得分:0)

使用属性字符串

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

//Calculating height on base of length of text
    UIFont *font =  [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
    NSAttributedString *attributedText =
    [[NSAttributedString alloc]
     initWithString:YOURSTRING //[NSString string withformat:@"\n Yourstring"] \n for: number of lines require for static label in cell
     attributes:@
     {
     NSFontAttributeName: font
     }];
    CGRect rect = [attributedText boundingRectWithSize:(CGSize){CELL_CONTENT_WIDTH, CGFLOAT_MAX}
                                               options:NSStringDrawingUsesLineFragmentOrigin
                                               context:nil];
    CGSize size = rect.size;

    //  NSString *height = [NSString stringWithFormat: @"%.2f", ceilf(size.height)+22];
    return (ceilf(size.height)+22)*0.6; //change 0.6 to 0.9 according to the difference in required and extra calculted height, it works for me every time
}

答案 3 :(得分:0)

使用此方法在单元格中标签的动态高度。并根据其他标签及其文字设置框架。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //Define Your Cell Here

    //Setting Properties of Labels Here Like this
    cell.countLabel.font = [UIFont systemFontOfSize:22];
    cell.countLabel.numberOfLines = 0;

    cell.detailLabel.font = [UIFont systemFontOfSize:22];
    cell.detailLabel.numberOfLines = 0;

    cell.descriptionLabel.numberOfLines = 0;
    cell.descriptionLabel.font = [UIFont systemFontOfSize:26];

    //Setting label's Text Here
    cell.countLabel.text = @"AAAAAA";
    cell.detailLabel.text = @"Number of people: ";
    cell.descriptionLabel.text = @"ABCDEFGHIJKLM";

    //Setting Label's Dynamic Height Using setlableFrame Method
    cell.countLabel = [self setlableFrame:cell.countLabel fontSize:22];
    cell.detailLabel = [self setlableFrame:cell.detailLabel fontSize:22];
    cell.descriptionLabel = [self setlableFrame:cell.descriptionLabel fontSize:26];

    //Setting Label's Frame According to Above Label
    cell.detailLabel.frame = CGRectMake(cell.detailLabel.frame.origin.x,       cell.countLabel.frame.size.height + cell.countLabel.frame.origin.y, cell.detailLabel.frame.size.width, cell.detailLabel.frame.size.height);

    cell.descriptionLabel.frame =  CGRectMake (cell.descriptionLabel.frame.origin.x, cell.detailLabel.frame.size.height + cell.detailLabel.frame.origin.y, cell.descriptionLabel.frame.size.width, cell.descriptionLabel.frame.size.height);

      return cell;
}

现在,对单元格的动态高度使用以下方法。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
     CGFloat myCellHeight = 0;//For Cell Height

     //For countLabel
     UILabel * myLabel = [[UILabel alloc] init];
     myLabel.frame = CGRectMake( 0, 0, 290, 21.0);//here set frame same as countLabel label's frame. make sure that, You must have setting width same as that label, otherwise you don't have accuracy for this.
     myLabel.numberOfLines = 0;
     myLabel.text = @"AAAAAA";
     myLabel = [self setlableFrame:myLabel fontSize:22.0];

     //Adding Height of countLabel
     myCellHeight = myLabel.frame.size.height;

     //For detailLabel
     myLabel.text = @"Number of people: ";
     myLabel = [self setlableFrame:myLabel fontSize:22.0];

     //Adding Height of detailLabel
     myCellHeight = myCellHeight + myLabel.frame.size.height;

     //For descriptionLabel
     myLabel.text = @"ABCDEFGHIJKLM";
     myLabel = [self setlableFrame:myLabel fontSize:26.0];

     //Adding Height of detailLabel
     myCellHeight = myCellHeight + myLabel.frame.size.height;

     return myCellHeight;
}

此方法用于根据文本制作标签的动态高度

//for setting the dynamic height of labels
-(UILabel *)setlableFrame:(UILabel*)myLabel fontSize:(float)size
{
    CGSize possibleSize = [myLabel.text sizeWithFont:[UIFont systemFontOfSize: size] constrainedToSize:CGSizeMake(myLabel.frame.size.width, 9999) lineBreakMode:NSLineBreakByWordWrapping];

    CGRect newFrame = myLabel.frame;
    newFrame.size.height = possibleSize.height;
    //If you want dynamic width of label, then uncomment following line
    //newFrame.size.width = possibleSize.width;
    myLabel.frame = newFrame;
    return myLabel;
}

答案 4 :(得分:0)

您不需要为动态高度做任何这些。只需通过给出高于等于标签最小高度的高度以及其间具有垂直对齐的其他对齐约束来设置所有标签的约束。另外,不要忘记为自动收缩属性设置 0 属性和固定字体。现在,当您将长文本分配给标签时,只需调用

即可
[self.view layoutIfNeeded];

并且标签会根据里面的文字增加它们的高度。