繁体   English   中英

在静态表格视图中动态调整文本视图和表格视图单元的大小

[英]Resize text view and table view cell dynamically in static table view

我有一个静态表格视图,正在用作我的应用程序的数据输入源。 其中一个单元格具有一个UITextView,该UITextView需要随着用户类型的变化而动态增长和缩小,以及与此同时逻辑上增长/缩小的单元格。 我读了很多关于此的文章,但是我认为我因为使用​​STATIC表视图而被抛出。

这是我调整大小的尝试(几乎完全失败):

- (void)textViewDidChange:(UITextView *)textView
{
    self.numberOfLines = (textView.contentSize.height / textView.font.lineHeight) - 1;

    float height = 44.0;
    height += (textView.font.lineHeight * (self.numberOfLines - 1));

    CGRect textViewFrame = [textView frame];
    textViewFrame.size.height = height - 10.0; //The 10 value is to retrieve the same height padding I inputed earlier when I initialized the UITextView
    [textView setFrame:textViewFrame];

    [self.tableView beginUpdates];
    [self.tableView endUpdates];

    [self.messageTextView setContentInset:UIEdgeInsetsZero];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    float height = 44.0;
    if (self.messageTextView) {
        height += (self.messageTextView.font.lineHeight * (self.numberOfLines - 1));
    }
    return height;
}

我会采取任何提示! 谢谢。

您无需执行任何操作,只需通过以下代码即可:-有一个新函数可以替换sizeWithFont,即boundingRectWithSize。

在我的项目中添加以下函数,该函数利用iOS7上的新功能以及低于7的iOS上的旧功能。它的语法与sizeWithFont基本相同:

   -(CGSize)text:(NSString*)text sizeWithFont:(UIFont*)font constrainedToSize:(CGSize)size{
        if(IOS_NEWER_OR_EQUAL_TO_7){
            NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                              font, NSFontAttributeName,
                                              nil];

            CGRect frame = [text boundingRectWithSize:size
                                              options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading)
                                           attributes:attributesDictionary
                                              context:nil];

            return frame.size;
        }else{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
            return [text sizeWithFont:font constrainedToSize:size];
#pragma clang diagnostic pop
        }
    }

您可以将IOS_NEWER_OR_EQUAL_TO_7添加到项目中的prefix.pch文件中,如下所示:

#define IOS_NEWER_OR_EQUAL_TO_7 ( [ [ [ UIDevice currentDevice ] systemVersion ] floatValue ] >= 7.0 )

我从此链接中引用:

在iOS 7中具有UITextView高度的UITableViewCell?

请检查我的答案。

计算UITableViewCell高度以适合字符串

这里的标签用于计算像元的高度。

完成文本字段中的文本编辑后- (void)textViewDidEndEditing:(UITextView *)textView调用tableview reloadData函数以根据在textview中输入的文本更新单元格

您将需要手动计算textView新大小,并手动为tableView调用update。

static CGFloat cellHeight = 85;//from story board height to
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
    if (indexPath.row == 0) {
        return cellHeight + [self appendTextViewNewHeight];
    }
    return cell.bounds.size.height;
}

-(CGFloat) appendTextViewNewHeight {
    return _appendHeightForTextView;
}

static CGFloat textViewHeightStartHeight = 33;//from storiboard height
- (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);
    if (newFrame.size.height >= textViewHeightStartHeight) {
        CGFloat newAppend = newFrame.size.height - textViewHeightStartHeight;
        if (newAppend != self.appendHeightForTextView) {
            self.appendHeightForTextView = newAppend;
            [self.tableView beginUpdates];
            [self.tableView endUpdates];
        }
    }
}

另外,您还需要在代码中的情节提要上设置委托。 在viedDidLoad中,您需要执行以下操作:

self.appendHeightForTextView = 0;   

最后在您的单元的情节提要中设置约束。 不要设置textView的底部约束。 仅设置右,左和上。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM