0
我正在嘗試爲實踐創建一個簡單的聊天應用程序。我使用UITableView來設置我的聊天室,並從故事板中配置我的單元格。我把一個簡單的UITextView作爲我的消息容器,然而由於一些奇怪的原因,我得到的尺寸很奇怪,沒有任何意義。這裏是該小區的代碼:UITextView,sizeToFit行事怪異
- (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;
}
這是結果我得到: 是任何人都熟悉這個問題?有誰知道原因?
編輯:我試過使用UILabel和一切工作正常,但UILabel沒有我需要的功能。
謝謝您的回答。出於好奇,你知道爲什麼它的形狀像在圖片中嗎? – user1563544
@ [user1563544](http://stackoverflow.com/users/1563544/user1563544)如果答案對您有用,然後養成投票並接受它的習慣。這會鼓勵其他人回答你的問題 –