1

我想让 timelabel 、 userlabel 和 textstring 在单元格中右对齐。这样当接收者发送一些消息时,它将在聊天应用程序中显示为正确。

我怎样才能做到这一点?

我的代码如下:

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

为此,我认为我必须将框架对齐为正确的..如何将右对齐与框架对齐?

4

1 回答 1

0

而不是这个: cell.textString.frame = CGRectMake(75, 14, size.width +20, size.height + 20);

将您的 x 原点 (75) 设置为 (tableView.frame.size.width - size.width) (如果需要,+20),这意味着转到最右边的点,而不是尽可能多地返回文本宽度并将文本放在那里. 希望我很清楚。

于 2013-05-31T09:02:23.447 回答