【问题标题】:UITextView sizeThatFits returns different size than boundingRectWithSizeUITextView sizeThatFits 返回与 boundingRectWithSize 不同的大小
【发布时间】:2015-09-10 07:30:55
【问题描述】:

需要UITextView 的所需高度。 sizeThatFits 返回更大,但比boundingRectWithSize 正确的高度。为什么存在差异?

在两个地方我需要知道高度。在cellForRowAtIndexPathheightForRowAtIndexPath 中。

我不认为总是在heightForRowAtIndexPath 中创建一个UITextView 只是为了知道需要什么高度。

你知道什么解决方法来计算UITextViewheightForRowAtIndexPath 中的高度?

【问题讨论】:

    标签: ios uitableview uitextview


    【解决方案1】:

    上个月UITableView遇到了类似的问题,我用boundingRectWithSize来计算大小,其实是正确的。然后我把它放到 UITextView 中。

    我犯的一些错误:

    1. 我忘记在计算和 UITextView 时设置相同的字体大小
    2. UITextView 有边距,我将在 heightForRowAtIndexPath 中手动添加,并将 textContainerInset 设置为相同。

    希望对你有帮助。

    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
        NSInteger section = indexPath.section;
        NSUInteger axisIndex = section - 2;
        yAxis *yAxisObj = self.yAxisInfoArray[axisIndex];
        boundingRect = [yAxisObj.yAxisDescription boundingRectWithSize:CGSizeMake(self.descriptionViewWidth, CGFLOAT_MAX)
                                                               options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading
                                                            attributes:@{NSFontAttributeName:self.contentFont}
                                                               context:nil];
    
    
        return boundingRect.size.height + TEXT_TOP_MARGIN + TEXT_BOTTOM_MARGIN;
    }
    
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString *cellId = @"ChartDescriptionCell";
        ChartDescriptionCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (!cell) {
            cell = [[ChartDescriptionCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
            cell.textView.bounces = NO;
            cell.textView.showsHorizontalScrollIndicator = NO;
            cell.textView.showsVerticalScrollIndicator = NO;
            cell.textView.font = self.contentFont;
            cell.textView.textColor = [UIColor colorWithHex:@"#333333"];
            cell.textView.textContainerInset = UIEdgeInsetsMake(TEXT_TOP_MARGIN, -5, TEXT_BOTTOM_MARGIN, -5);
        }
        NSInteger section = indexPath.section;
        NSUInteger axisIndex = section - 2;
        yAxis *yAxisObj = self.yAxisInfoArray[axisIndex];
        cell.textView.text = yAxisObj.yAxisDescription;
        }
        return cell;
    }
    

    【讨论】:

    • 谢谢,关键是:textView.textContainerInset = UIEdgeInsetsZero;
    【解决方案2】:

    boundingRectWithSize 返回文本的大小,因此您应该手动提供字体。

    sizeThatFits 返回带有此文本的 UITextView 的大小

    如果您指向 iOS 8 及更高版本,您可以使用非常简单的动态单元格高度。对于 iOS 7,您需要一些解决方法。

    教程:http://www.raywenderlich.com/87975/dynamic-table-view-cell-height-ios-8-swift

    相关问题有很好的答案:Using Auto Layout in UITableView for dynamic cell layouts & variable row heights

    【讨论】:

    • 您知道如何在内容更改时更新textView 高度吗? stackoverflow.com/questions/32497229/…
    • @János 尝试使用textViewDidChange 委托方法并每次计算新高度。
    • @János 我不明白你的意思。如果你想要动态 UITableViewCell 高度,你应该使用约束。例如,您可以为 UITextView 设置 100,如果文本大于 100,则需要更新 UITextView 的高度约束
    • 需要限制,但不需要高度限制,请查看此帖子中的图片:stackoverflow.com/questions/18746929/…
    猜你喜欢
    • 2014-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-25
    • 2019-06-17
    • 1970-01-01
    相关资源
    最近更新 更多