簡體   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