iOS根据textView中的内容来确定表行的大小[英] iOS size table row based on content in textView

本文是小编为大家收集整理的关于iOS根据textView中的内容来确定表行的大小的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在尝试将表中的一行大小缩小到文本视图的高度,以便用户不必滚动文本视图,而是滚动表.到目前为止,我已经实现的目的似乎在大多数情况下都可以使用.我该如何正确构架?

代码:

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    // Set the textview frame
    CGRect frame = self.contentTextView.frame;
    frame.size.height = [self.summary boundingRectWithSize:CGSizeMake(280, 0) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:@{NSFontAttributeName:self.contentTextView.font} context:nil].size.height + 60;
    self.contentTextView.frame = frame;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return the height for the rows
    if (indexPath.section == 0) {
        return 60;
    }
    if (indexPath.section == 3) {

        // Size content row based on text
        return [self.summary boundingRectWithSize:CGSizeMake(280, 0) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:@{NSFontAttributeName:self.contentTextView.font} context:nil].size.height + 100;
    }
    return 44;
}

推荐答案

尝试此

- (CGFloat)tableView:(UITableView *)tv heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString* str = ... // here is a string which will be in text view

    CGSize boundingSize = CGSizeMake(textView.frame.size.width - 20, CGFLOAT_MAX);

    CGSize textSize = [str sizeWithFont:[UIFont systemFontOfSize:13] constrainedToSize:boundingSize lineBreakMode:NSLineBreakByWordWrapping];

    return textSize.height + 40; // add aditional space for margins
}

本文地址:https://www.itbaoku.cn/post/2091191.html

问题描述

I am trying to size a row in my table to the height of a textView so that the user does not have to scroll the textView, but rather scrolls the table. What I have implemented so far seems to work most of the time although sometimes the row height is a little too small, forcing the user to scroll the textview a little, or the row height is too much create a lot of whitespace after the text ends. How can I frame it just right?

Code:

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    // Set the textview frame
    CGRect frame = self.contentTextView.frame;
    frame.size.height = [self.summary boundingRectWithSize:CGSizeMake(280, 0) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:@{NSFontAttributeName:self.contentTextView.font} context:nil].size.height + 60;
    self.contentTextView.frame = frame;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return the height for the rows
    if (indexPath.section == 0) {
        return 60;
    }
    if (indexPath.section == 3) {

        // Size content row based on text
        return [self.summary boundingRectWithSize:CGSizeMake(280, 0) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:@{NSFontAttributeName:self.contentTextView.font} context:nil].size.height + 100;
    }
    return 44;
}

推荐答案

try this

- (CGFloat)tableView:(UITableView *)tv heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString* str = ... // here is a string which will be in text view

    CGSize boundingSize = CGSizeMake(textView.frame.size.width - 20, CGFLOAT_MAX);

    CGSize textSize = [str sizeWithFont:[UIFont systemFontOfSize:13] constrainedToSize:boundingSize lineBreakMode:NSLineBreakByWordWrapping];

    return textSize.height + 40; // add aditional space for margins
}