【问题标题】:IOS Resizing UITextView for IM ProgramIOS 为 IM 程序调整 UITextView 的大小
【发布时间】:2013-05-14 23:52:10
【问题描述】:

目前我有一个程序可以显示用户之间的聊天消息。它将每条消息放在一个单元格中,并将消息存储在单元格中包含的 UITextView 中。 UITextView 具有应用于视图的背景。如何调整文本视图的大小以调整背景大小并将动态内容正确放置在侧面?此外,您会推荐什么是随着视图调整单元格大小的最佳方式。我知道有很多类似的帖子,但我似乎找不到任何适用于这种特定情况的东西。他们要么在没有视图和背景的情况下调整内容的大小,要么在调整单元格大小时出现故障并且与文本视图内容的大小不一致。这是我在 cellForRowAtIndexPath 中使用的代码。

- (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;
}

【问题讨论】:

    标签: ios uitableview ios6 resize uitextview


    【解决方案1】:

    查看免费的 Sensible TableView 框架。有一个文本视图,可以随着内容的增长自动调整大小。

    【讨论】:

    • 我最终使用了一个源自 hpgrowthtextview、dana0550 的建议和 DharaParekh 的解决方案的解决方案。但是,我对苹果重用细胞的方式有疑问。我现在正在关注合理的 tableview 框架率。从我所见,这可能正是我正在寻找的。它有多少年了,它仍然受支持吗? @马特
    • 不确定它的年龄,但它目前非常活跃。您应该可以通过在他们的论坛中查找旧帖子或询问那里的聊天室中的人来知道。我已经使用它快一年了,我真的很高兴。
    【解决方案2】:

    您可以使用 NSString 类的以下任何方法来计算 textView 的大小,并从那里动态调整 textView 框架的大小。例如:

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

    如果您阅读 NSString 类参考,还有许多其他方法可以计算大小。

    【讨论】:

      【解决方案3】:

      使用hpgrowingtextview 调整文本视图的大小。

      并使用EditableViewCell 用于根据文本大小调整单元格大小

      OR 用于可编辑的表格视图单元格

      编写以下方法来重新加载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);
      }
      

      【讨论】:

        【解决方案4】:

        我为此创建了 UITextView 的子类:

        https://github.com/MatejBalantic/MBAutoGrowingTextView

        它是一个基于自动布局的轻量级 UITextView 子类,它会根据用户输入的大小自动增长和缩小,并且可以通过最大和最小高度来限制 - 所有这些都无需一行代码。

        主要用于界面生成器,仅适用于自动布局。

        这将解决 UITextView 的大小调整问题。为了确保表格视图单元格也被调整大小,分配一个 UITextView 委托并在其中定义此方法:

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

        使用它,您将能够检测每次输入/删除文本的时间,并确保 UITextView 的新大小也适用于 tableview 单元格。

        【讨论】:

          猜你喜欢
          • 2012-12-26
          • 2010-10-18
          • 1970-01-01
          • 1970-01-01
          • 2013-10-20
          • 1970-01-01
          • 1970-01-01
          • 2014-11-05
          • 2013-10-09
          相关资源
          最近更新 更多