1
我想在單元格中使時間標籤,用戶標籤和文本字符串右對齊。因此,當接收者發送一些消息時,它將在聊天應用程序中正確顯示。使文本視圖和標籤右對齊UITableviewcell
我該怎麼做?
我的代碼是如下:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
chatCell *cell = (chatCell *)[tableView dequeueReusableCellWithIdentifier:CHAT_CELL_IDENTIFIER];
NSUInteger row = indexPath.row;
if (row < chatData.count)
{
NSString *chatText = [[chatData objectAtIndex:row] objectForKey:TEXT];
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
UIFont *font = [UIFont systemFontOfSize:14];
CGSize size = [chatText sizeWithFont:font constrainedToSize:CGSizeMake(225.0f, 1000.0f) lineBreakMode:NSLineBreakByCharWrapping];
cell.textString.frame = CGRectMake(75, 14, size.width +20, size.height + 20); // set text frame
cell.textString.font = [UIFont fontWithName:FONT_NAME size:FONT_SIZE]; // set text font
cell.textString.text = chatText; // set text
[cell.textString sizeToFit];
NSDate *theDate = [[chatData objectAtIndex:row] objectForKey:DATE];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:DATE_FORMAT];
NSString *timeString = [formatter stringFromDate:theDate];
cell.timeLabel.text = timeString; // set timeLabel to display date and time
cell.userLabel.text = [[chatData objectAtIndex:row] objectForKey:NAME]; // set userLabel to display userName
if ([self.user.userName isEqualToString:cell.userLabel.text]) {
cell.backgroundView = [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"bubbleSomeone.png"] stretchableImageWithLeftCapWidth:21 topCapHeight:14]];
}
else
{
cell.backgroundView = [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"bubbleMine.png"] stretchableImageWithLeftCapWidth:21 topCapHeight:14]];
}
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellText = [[chatData objectAtIndex:indexPath.row] objectForKey:TEXT];
UIFont *cellFont = [UIFont fontWithName:FONT_NAME size:FONT_SIZE];
CGSize constraintSize = CGSizeMake(225.0f, MAXFLOAT);
CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];
return labelSize.height + 40;
}
爲此,我想我有對齊的幀作爲right..How我可以對齊rightalignment到幀??
謝謝。有用。 – Ponting