2013-05-31 32 views
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到幀??

回答

0

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

將您的x原點(75)設置爲(tableView.frame.size.width - size.width)(+20,如果需要) 這意味着去到最正確的點,而不是根據需要爲文本寬度並把文字放在那裏。 希望我清楚。

+1

謝謝。有用。 – Ponting