uilabel 视图未右对齐
uilabel view not aligning to right
我已经试过了
textLabel.textAlignment =NSTextAlignmentRight;
但我的代码似乎没有任何效果。里面的 cellforrawatindexpath
- (UITableViewCell *)tableView:(UITableView *)myTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = (UITableViewCell *)[self.messageList dequeueReusableCellWithIdentifier:@"ChatListItem"];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"OutGoing" owner:self options:nil];
}
cell = (UITableViewCell *)[nib objectAtIndex:0];
}
textLabel = (UILabel *)[cell viewWithTag:1];
textLabel.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"chat_b2.png"]];
textLabel =[[UILabel alloc] initWithFrame:CGRectMake(21,12, 241,30)];
NSString * test=[[rssOutputData objectAtIndex:indexPath.row]xml_msg];
textLabel.text = test;
textLabel.numberOfLines = 0;
[textLabel sizeToFit];
[cell addSubview:textLabel];
return cell;
}
截图如下:
在这里你可以看到(红色)文本左对齐我希望它右对齐请帮助我解决这个问题。
使用 [textLabel sizeToFit]
标签框的大小将适合文本,因此您的标签宽度将与文本相同,右对齐将使文本留在原位。评论这一行,它将起作用。
编辑:
[textLabel sizeToFit];
[textLabel setFrame:CGRectMake(21,12,241,textLabel.frame.size.height)];
如果您使用的是 sizetoFit
,那么您的标签大小将根据内容大小而减小。这不是对齐问题。删除此行,然后添加 textLabel.textAlignment =NSTextAlignmentRight;
并检查它应该工作我认为。
您总是将 textLabel 框架设置为以下框架:
textLabel =[[UILabel alloc] initWithFrame:CGRectMake(21,12, 241,30)];
所以只有标签出现在左边。尝试根据 send/Receive 消息更改标签框。
希望对您有所帮助..
你的标签是对你给出的框架的约束
textLabel =[[UILabel alloc] initWithFrame:CGRectMake(21,12, 241,30)];
因此它与您提供的框架右对齐,即您的标签与特定宽度对齐,即 30 ......您应该让标签的宽度等于单元格,然后它会根据您的单元格右对齐。
我已经试过了
textLabel.textAlignment =NSTextAlignmentRight;
但我的代码似乎没有任何效果。里面的 cellforrawatindexpath
- (UITableViewCell *)tableView:(UITableView *)myTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = (UITableViewCell *)[self.messageList dequeueReusableCellWithIdentifier:@"ChatListItem"];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"OutGoing" owner:self options:nil];
}
cell = (UITableViewCell *)[nib objectAtIndex:0];
}
textLabel = (UILabel *)[cell viewWithTag:1];
textLabel.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"chat_b2.png"]];
textLabel =[[UILabel alloc] initWithFrame:CGRectMake(21,12, 241,30)];
NSString * test=[[rssOutputData objectAtIndex:indexPath.row]xml_msg];
textLabel.text = test;
textLabel.numberOfLines = 0;
[textLabel sizeToFit];
[cell addSubview:textLabel];
return cell;
}
截图如下:
在这里你可以看到(红色)文本左对齐我希望它右对齐请帮助我解决这个问题。
使用 [textLabel sizeToFit]
标签框的大小将适合文本,因此您的标签宽度将与文本相同,右对齐将使文本留在原位。评论这一行,它将起作用。
编辑:
[textLabel sizeToFit];
[textLabel setFrame:CGRectMake(21,12,241,textLabel.frame.size.height)];
如果您使用的是 sizetoFit
,那么您的标签大小将根据内容大小而减小。这不是对齐问题。删除此行,然后添加 textLabel.textAlignment =NSTextAlignmentRight;
并检查它应该工作我认为。
您总是将 textLabel 框架设置为以下框架:
textLabel =[[UILabel alloc] initWithFrame:CGRectMake(21,12, 241,30)];
所以只有标签出现在左边。尝试根据 send/Receive 消息更改标签框。
希望对您有所帮助..
你的标签是对你给出的框架的约束
textLabel =[[UILabel alloc] initWithFrame:CGRectMake(21,12, 241,30)];
因此它与您提供的框架右对齐,即您的标签与特定宽度对齐,即 30 ......您应该让标签的宽度等于单元格,然后它会根据您的单元格右对齐。