UITextview SizetoFit在UITableView中对齐

时间:2013-09-25 00:05:33

标签: alignment uitextview scale

快速提问,是否可以对齐已启用Sizetofit的UITextField?说对了吗?

我有一个聊天对话窗口(UITableView),并使用Sizetofit帮助为我的实际消息创建这些“泡泡”类型的对话(picture => Picture of my Conversation Chat Window, where the UITextField, MessageLabel has size to fit, but does not align RIGHT, for the user who is currently signed in.

任何帮助都会非常感激,让这些气泡向右:-)

这是一个准系统片段....

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

 {
static NSString *cellIdentifier = @"HistoryCell";
MessageCustomCellCell *cell = (MessageCustomCellCell *)[self.tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
    cell.messageLabel.layer.cornerRadius = 9;
    cell.messageLabel.clipsToBounds = YES;
}

NSSortDescriptor *ratingsSortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:YES];
NSArray *sortedRows = [_tableData sortedArrayUsingDescriptors:[NSArray arrayWithObject:ratingsSortDescriptor]];
_tableData = sortedRows;
NSDictionary *item = [_tableData objectAtIndex:[indexPath row]];


 // loggedInUser
 if ([[item objectForKey:@"fromuser"] isEqualToString:loggedInUser]) {

    cell.messageLabel.text = [item objectForKey:@"message"];
    cell.messageLabel.backgroundColor = [UIColor greenColor];
    cell.messageLabel.frame = CGRectMake(10, 25, 246, cell.messageLabel.contentSize.height);
    [cell.messageLabel sizeToFit];

 } else {
 // MESSAGE IS FRIEND OF THE PERSON SIGNED IN
     cell.messageLabel.text = [item objectForKey:@"message"];
     cell.messageLabel.backgroundColor = [UIColor redColor];
     cell.messageLabel.frame = CGRectMake(65, 25, 250, cell.messageLabel.contentSize.height);
     [cell.messageLabel sizeToFit];

 }

我想一种方法是找出文本的实际宽度,或计算它,并相应地调整和定位,但不确定如何。

谢谢!

1 个答案:

答案 0 :(得分:0)

这是我的榜样。

(void)initWithMessage:(NSString*)message {
    [self.messageView setText:message];
    self.messageView.textColor = [UIColor whiteColor];

    [self.messageView sizeToFit];

    CGRect myFrame = self.messageView.frame;
    myFrame.origin.x = self.frame.size.width - myFrame.size.width;
    [self.messageView setFrame:myFrame];
}