【问题标题】:setting the size of UITextView and UITableViewCell depending on Content根据 Content 设置 UITextView 和 UITableViewCell 的大小
【发布时间】:2014-04-18 16:48:33
【问题描述】:

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

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

我正在尝试根据内容调整 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;
}

谁能指出我正确的方向。

【问题讨论】:

    标签: ios objective-c uitableview


    【解决方案1】:

    尝试在这里找到解决方案 - 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;
    }
    

    【讨论】:

      【解决方案2】:

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

      这个方法接受一个 NSString 的文本并返回一个 CGRect,该 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: 方法中重用它。

      【讨论】:

      • 感谢您的回复。但我仍然有这个问题!在滚动表格之前,我的 UITextView 不会调整到文本的大小。然后当我滚动时,一些有趣的事情开始发生——一些文本被有效地包装,所以它仍然被剪切,所有的文本都被填充在我的单元格的左上角。有什么想法吗?
      猜你喜欢
      • 2014-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-10
      • 2015-03-27
      • 2017-08-24
      • 2016-03-13
      • 1970-01-01
      相关资源
      最近更新 更多