2013-05-14 29 views
0

目前我有一個程序顯示用戶之間的聊天消息。它將每條消息放入一個單元格中,並將該消息存儲在單元格中包含的UITextView中。 UITextView具有應用於視圖的背景。我如何調整文本視圖的大小以便調整背景大小並將動態內容正確放置在側面?此外,你會推薦什麼樣的方式來調整單元格的大小和視圖。我知道有很多類似的帖子,但我似乎無法找到適用於這種特定情況的任何內容。要麼他們調整內容的大小,而沒有視圖和背景,或者當單元格調整大小時,它會出現問題,並且與文本視圖內容的大小不一致。這裏是使用cellForRowAtIndexPath內部的代碼。iOS調整UITextView的IM程序

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    MessageCell *cell; 
    Message *message = [_messages objectAtIndex:(indexPath.row)]; 

    if ([[[PFUser currentUser] objectId] isEqualToString:message.sender]) 
    { 
     cell = (MessageCell *)[tableView dequeueReusableCellWithIdentifier:@"SenderCell"]; 
     cell.message.text = message.message; 
    } 
    else 
    { 
     cell = (MessageCell *)[tableView dequeueReusableCellWithIdentifier:@"RecipientCell"]; 
     cell.message.text = message.message; 
    } 

    return cell; 
} 

回答

0

簽出免費的Sensible TableView框架。隨着內容的增長,文本視圖會自動調整大小。

+0

我最終使用了從hpgrowingtextview,dana0550的建議和DharaParekh的解決方案派生的解決方案。然而,即時通訊問題與他們的方式蘋果重用細胞。我現在正在觀察明智的tableview框架費率。從我所看到的這可能正是我所期待的。它多大了,它仍然支持? @Matt – ScottOBot

+0

不知道多大年紀,但它目前非常活躍。您應該可以通過在論壇中查找舊帖子或向該聊天室的人詢問。我已經使用了近一年了,我真的很開心。 – Matt

+0

順便說一句,感謝hpgrowingtextview,我會檢查一下! :) – Matt

0

您可以使用NSString類的以下任何方法來計算textView大小,並從那裏動態調整textView框的大小。例如:

textView.text sizeWithFont:[UIFont fontWithName:@"" size:18.0]]; 

如果您閱讀NSString類的引用,還有許多其他方法可以計算大小。

0

使用hpgrowingtextview調整textview的大小。

,並使用EditableViewCell 細胞尺寸調整基於文本大小

OR爲可編輯的tableview細胞重新加載的UITableView的數據

寫下面的方法。爲此,您使用uitableview的自定義單元格。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return [arrChatMessage count]; 
} 

-(CGFloat)tableView:(UITableView *)aTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSString *textc1 = [arrChatMessage objectAtIndex:indexpath.row]; 
    CGSize constraintc1 = CGSizeMake(320, 2000); 
    //320 means width of your label in which you want to display chat message 
    CGSize sizec1 = [textc1 sizeWithFont:[UIFont fontWithName:@"Arial-BoldMT" size:12.0] constrainedToSize:constraintc1 lineBreakMode:UILineBreakModeWordWrap]; 
    //font and font size of your label in which you want to display chat message 
    return sizec1.height; 
} 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    ChatListCustomCell *cell = (ChatListCustomCell *)[tblChatList dequeueReusableCellWithIdentifier:@"ChatListCustomCell"]; 

    if (cell != nil) 
     cell = nil; 

    NSArray* nib = [[NSBundle mainBundle] loadNibNamed:@"ChatListCustomCell" owner:self options:nil]; 
    cell = [nib objectAtIndex:0]; 
    cell.showsReorderControl = NO; 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    cell.backgroundColor=[UIColor clearColor]; 

    //.............. 

    NSString *textc1 = [arrChatMessage objectAtIndex:indexpath.row]; 
    CGSize constraintc1 = CGSizeMake(320, 2000); 
    //320 means width of your label in which you want to display chat message 
    CGSize sizec1 = [textc1 sizeWithFont:[UIFont fontWithName:@"Arial-BoldMT" size:12.0] constrainedToSize:constraintc1 lineBreakMode:UILineBreakModeWordWrap]; 
    //font and font size of your label in which you want to display chat message 

    cell.lblChatMessage.text = [arrChatMessage objectAtIndex:indexpath.row]; 
    cell.lblChatMessage.frame = CGRectMake(0,0,320,sizec1.height); 
} 
0

我提出的UITextView的只是該子類:

https://github.com/MatejBalantic/MBAutoGrowingTextView

它是一種基於自動佈局重量輕的UITextView子類,可以自動地增大和收縮基於用戶輸入的尺寸,並且可以是受最大和最小高度約束 - 全部沒有一行代碼。

主要用於界面構建器,僅適用於自動佈局。

這將解決調整UITextView的大小。爲了確保還表視圖細胞大小,分配一個UITextView委託和定義在它這個方法:

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text 

使用,你就可以發現每一次的文本被輸入/刪除,比確保的UITextView的新的大小也適用於tableview單元格。