【问题标题】:Cannot change the size of UITextView无法更改 UITextView 的大小
【发布时间】:2013-10-11 13:24:35
【问题描述】:

我通过 Interface Builder 向 tableView 添加了一些 tableViewCells,其中一个包含 UITextView。

当按钮被点击时,tableView 被重新加载:

- (void)updateViewWithMessage:(NSString *)message
{
    self.someMessage = message;
    [self.tableView reloadData];
}

在我的 cellForRowAtIndexPath 方法中,我检查此单元格并执行以下操作:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CellIdentifier = kMyCustomCell;
    MyCustomCell *contentCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    CGRect textViewFrame = contentCell.contentTextView.frame;
    textViewFrame.size.height = [self heightForContentView];

    NSLog(@"%f, %f, %f, %f\n", textViewFrame.origin.x,
          textViewFrame.origin.y,
          textViewFrame.size.width,
          textViewFrame.size.height);

    contentCell.contentTextView.frame = textViewFrame;
    contentCell.contentTextView.text = self.someMessage;

    return contentCell;
}

在 heightForContentView() 我计算高度:

- (CGFloat)heightForContentView
{
    CGSize boundingRectSize = CGSizeMake(kCustomCellWidth - 20, CGFLOAT_MAX);
    CGRect textViewRect = [self.someMessage boundingRectWithSize:boundingRectSize
                                                     options:NSStringDrawingUsesLineFragmentOrigin
                                              attributes:nil
                                                 context:nil];
    return textViewRect.size.height + 20;
}

如果我将行高度设置为某个值,则单元格会正确调整大小。但是里面的textView没有。 如果我单击按钮两次(再次调用 updateViewWithMessage: 方法)然后它会调整大小 - 这很奇怪!

顺便说一句:NSLog 打印的大小是正确的,所以肯定是设置好了,但是为什么这个 TextView 没有调整大小呢?

谁能帮我快速解决这个问题?

【问题讨论】:

    标签: uitableview interface-builder ios7 uitextview


    【解决方案1】:

    在 cmets 之后编辑:

    我认为您没有调整 UITextView 的大小,请在调整大小之前添加此代码contentCell

    contentTextView.frame = CGRectMake(CGRectGetMinX(contentTextView.frame), CGRectGetMinY(contentTextView.frame), CGRectGetWidth(contentTextView.frame), textViewFrame.size.height);
    

    旧答案:

    也许你忘了传递attributes 来计算新的大小:

    CGRect textViewRect = [self.someMessage boundingRectWithSize:boundingRectSize
                                                 options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading)
                                              attributes:@{NSFontAttributeName:FONT}
                                                 context:nil];
    

    FONT 是你当前的字体。

    看看here

    【讨论】:

    • 您的单元格从您的 uitextview 的新大小正确调整大小,但您的 textview 没有调整大小?
    • 是的,我可以根据需要将单元格大小调整为高度 - 好的。但是我不能改变它包含的 UITextView 的大小,它只有在我在界面生成器中改变它时才会改变。但我想让它更加动态......我以编程方式对 TextView 所做的更改对其大小没有任何影响。
    • 试试这个选项 (NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading)
    • 不,也不行。将新框架分配给 TextView 的属性也应该完成这项工作。但奇怪的是 - 一旦我调用 updateViewWithMessage: textView 不会调整大小。如果我第二次调用它...
    • 嗯,我觉得你的[self.tableView reloadData]; 不太好用。
    猜你喜欢
    • 2013-10-25
    • 1970-01-01
    • 2012-07-20
    • 2011-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多