快速提问,是否可以对齐已启用Sizetofit的UITextField?说对了吗?
我有一个聊天对话窗口(UITableView),并使用Sizetofit帮助为我的实际消息创建这些“泡泡”类型的对话(picture =>
任何帮助都会非常感激,让这些气泡向右:-)
这是一个准系统片段....
- (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];
}
我想一种方法是找出文本的实际宽度,或计算它,并相应地调整和定位,但不确定如何。
谢谢!
答案 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];
}