根据内容设置UITextView和UITableViewCell的大小

时间:2014-04-18 16:48:33

标签: ios objective-c uitableview

大家好我正在我的应用上构建聊天功能。

在我的每个自定义表格单元格中,我都有一个包含用户注释的UITextView。

我正在尝试根据内容调整UITextView的大小,然后调整单元格的高度。

我遇到的第一个问题是当我尝试使用sizeToFit调整UITextView的大小时。由于某种原因,它使UITextView的宽度有时非常狭窄,通常根本不起作用。

这是我到目前为止的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"ClubChatCell";
    ClubChatCell *cell = (ClubChatCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];


    if (cell == nil) {
        cell = [[ClubChatCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    ClubDetails *club = nil;

    club = [_clubs objectAtIndex:indexPath.row];

    [cell.commentText setScrollEnabled:YES];
    cell.commentText.text = club.comment;
    [cell.commentText sizeToFit];
    [cell.commentText setScrollEnabled:NO];

    cell.fullNameLabel.text = club.creator;

    cell.profilePic.image = club.creatorProfilePic;
    cell.profilePic.layer.cornerRadius = cell.profilePic.frame.size.height /2;
    cell.profilePic.layer.masksToBounds = YES;
    cell.profilePic.layer.borderWidth = 0;
    cell.timeLabel.text = club.commentDate;



    return cell;
}

然后是TableViewCell的高度:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ClubDetails *club = nil;
    club = [_clubs objectAtIndex:indexPath.row];

    NSString *cellText = club.comment;
    UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:14.0];
    CGSize constraintSize = CGSizeMake(225.0f, MAXFLOAT);
    CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];

    return labelSize.height + 80;
}

有人可以指出我正确的方向。

2 个答案:

答案 0 :(得分:0)

尝试在此处找到解决方案 - How do I size a UITextView to its content?

有解决方案:

 CGRect frame = _textView.frame;
frame.size.height = _textView.contentSize.height;
_textView.frame = frame;

对于我的应用,我使用了这个解决方案:

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

答案 1 :(得分:0)

我最近遇到了这个问题,并且能够使用boundingRectWithSize:options:attributes:context

来解决这个问题

此方法接受NSString的文本并返回适合该文本的CGRect和提供的属性。

您可以类似地使用它:

NSString *text = @"This is my text. It could be any length";
CGSize maxLabelSize = CGSizeMake(280, FLT_MAX); 
CGRect rectForTextView = [text boundingRectWithSize:maxLabel Sizeoptions:NSStringDrawingUsesLineFragmentOrigin attributes:textAttributes context:nil];

CGSize变量是CGRect / TextView的约束条件。在这种情况下,我做了它,使其宽度最大为280,高度将是最大浮动值。将高度作为最大值将允许它几乎无限地扩展。显然,您可以将这些值设置为您想要的任何值。

接下来要做的就是获取CGRect'rectForTextView'的高度并将其分配给CGFloat。

CGFloat cellHeight = CGRectGetHeight(rectForTextView); 

现在分配给cellHeight变量的高度是您想要设置tableView的行高和textView高度的高度。

请记住,您可能希望为文本添加一些额外的边距空间,因为它实际上是约束给定文本所需的精确高度。

我创建了一个执行此操作的基本类方法,以便我可以在tableView:heightForRowAtIndexPath:方法中轻松地重用它。