使文本视图和标签右对齐UITableviewcell

问题描述:

我想在单元格中使时间标签,用户标签和文本字符串右对齐。因此,当接收者发送一些消息时,它将在聊天应用程序中正确显示。使文本视图和标签右对齐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到帧??

取而代之: cell.textString.frame = CGRectMake(75,14,size.width + 20,size.height + 20);

将您的x原点(75)设置为(tableView.frame.size.width - size.width)(+20,如果需要) 这意味着去到最正确的点,而不是根据需要为文本宽度并把文字放在那里。 希望我清楚。

+1

谢谢。有用。 – Ponting