0

I'm trying to create a simple chat app for practice. I use UITableView to setup my chatroom and I configured my cell from the storyboard. I put a simple UITextView as my message container, however for some odd reason the sizes I get are weird and make no sense. here's the code for the cell:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ChatroomTableViewCell *cell = (ChatroomTableViewCell *)[self.tableView dequeueReusableCellWithIdentifier:@"MessageCell" forIndexPath:indexPath];
    Message *cellMessage = [self.messages objectAtIndex:indexPath.row];

    cell.userNameLabel.text = cellMessage.userName;
    cell.messageLabel.text = cellMessage.messageText;
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"HH:mm a"];
    [cell.messageLabel.layer setCornerRadius:2.0f];

    if (cellMessage.messageType == SENT_MESSAGE) {
        NSString *dateLabelString = [NSString stringWithFormat:@"Sent: %@", [formatter stringFromDate:cellMessage.relevantDate]];
        cell.relevantDateLabel.text = dateLabelString;
        cell.userNameLabel.textAlignment = NSTextAlignmentRight;
        cell.relevantDateLabel.textAlignment = NSTextAlignmentRight;
        [cell.messageLabel setBackgroundColor:self.currentTheme.sentMessageColor];
        [cell.messageLabel.layer setBorderColor:self.currentTheme.sentMessageColor.CGColor];
        [cell.messageLabel setTextAlignment:NSTextAlignmentRight];
        [cell.messageLabel sizeToFit];
        [cell.messageLabel setFrame:CGRectMake(self.view.frame.size.width - 20 - cell.messageLabel.frame.size.width - 10, cell.messageLabel.frame.origin.y, cell.messageLabel.frame.size.width + 10, cell.messageLabel.frame.size.height + 5)];
    } else {
        NSString *dateLabelString = [NSString stringWithFormat:@"Received: %@", [formatter stringFromDate:cellMessage.relevantDate]];
        cell.relevantDateLabel.text = dateLabelString;
        cell.userNameLabel.textAlignment = NSTextAlignmentLeft;
        cell.relevantDateLabel.textAlignment = NSTextAlignmentLeft;
        if (cellMessage.messageType == EVENT_MESSAGE) {
            [cell.messageLabel setBackgroundColor:self.currentTheme.eventMessageColor];
        } else {
            [cell.messageLabel setBackgroundColor:self.currentTheme.receivedMessageColor];
        }
        [cell.messageLabel sizeToFit];
    }
    return cell;
}

This is the result I get: enter image description here Is anyone familiar with this problem? Does anyone know the reason?

EDIT: I tried using UILabel and everything worked fine, but UILabel doesn't have the functionality I need.

user1563544
  • 379
  • 3
  • 17

1 Answers1

0

The problem is that sizeToFit doesn't do, for a UITextView, what you want done. You can make a UITextView that is sized to its contents, but that's not how to do it.

Do a search on stack overflow, you'll find lots of info. For example: How do I size a UITextView to its content?

Community
  • 1
  • 1
matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Thank you for your answer. Just out of curiosity, do you know why it's shaped like in the picture? – user1563544 Mar 24 '14 at 19:14
  • @[user1563544](http://stackoverflow.com/users/1563544/user1563544) If answers are useful to you then make habit to up vote and accept it. This will encourage the others to answer your question – Vishal Sharma Apr 01 '14 at 12:59