ios UILabel 文本右对齐
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11228637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UILabel text alignment right
提问by Alok Srivastava
I want that my text should be align right.
我希望我的文字应该正确对齐。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"lisn"];
cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"lisn"] autorelease];
CGSize textSize = { 210.0, 10000.0 };
CGSize size = [[gMessageArray objectAtIndex:indexPath.row] sizeWithFont:[UIFont systemFontOfSize:12] constrainedToSize:textSize lineBreakMode:UILineBreakModeWordWrap];
UILabel *lisnerMessage=[[[UILabel alloc] init] autorelease];
lisnerMessage.backgroundColor = [UIColor clearColor];
[lisnerMessage setFrame:CGRectMake(75 ,20,size.width + 5,size.height+2)];
lisnerMessage.numberOfLines=0;
lisnerMessage.textAlignment=UITextAlignmentRight;
lisnerMessage.text=[gMessageArray objectAtIndex:indexPath.row];
[cell.contentView addSubview:lisnerMessage];
return cell
}
but my text is not align right Please Help
但我的文字没有对齐请帮助
回答by Ryan Poolos
Because you are using sizeWithFont and then setting your frame to that size, your text is aligned right. Try added a background color of light gray to your label to see what I'm talking about. Your label should be set to the same size as your table cell and allow the text to flow inside it. Then it will align to the right.
因为您使用 sizeWithFont 然后将您的框架设置为该大小,所以您的文本会正确对齐。尝试在标签中添加浅灰色背景色,看看我在说什么。您的标签应设置为与表格单元格相同的大小,并允许文本在其中流动。然后它会向右对齐。
Update with sample
用样本更新
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"lisn"];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"lisn"];
UILabel *lisnerMessage = [[UILabel alloc] init];
lisnerMessage.backgroundColor = [UIColor clearColor];
[lisnerMessage setFrame:cell.frame];
lisnerMessage.numberOfLines = 0;
lisnerMessage.textAlignment = NSTextAlignmentRight;
lisnerMessage.text = [gMessageArray objectAtIndex:indexPath.row];
[cell.contentView addSubview:lisnerMessage];
return cell
}
回答by Vineesh TP
Right alignment for label
标签右对齐
yourLabel.textAlignment = NSTextAlignmentRight;
回答by Alok Srivastava
Finally I have fix my problem. I was doing small mistake
最后我解决了我的问题。我犯了一个小错误
[lisnerMessage setFrame:CGRectMake(75 ,20,size.width + 5,size.height+2)];
I just remove size.width
and give my specific coordinate 200 after that the text is align right.
size.width
在文本右对齐之后,我只是删除并给出我的特定坐标 200。
[lisnerMessage setFrame:CGRectMake(75 ,20,200,size.height+2)];
Thanks all for your response
谢谢大家的回复
回答by Dustin
Why don't you just make the label in interface builder/storyboard and select the "align right" option? Then connect it as a property named lisnerMessage and lisnerMessage.text=[gMessageArray objectAtIndex:indexPath.row];
That would significantly cut down on how much code you're writing and definitely work.
为什么不直接在界面构建器/故事板中制作标签并选择“右对齐”选项?然后将其连接为名为 lisnerMessage 的属性,lisnerMessage.text=[gMessageArray objectAtIndex:indexPath.row];
这将显着减少您正在编写的代码量,并且绝对有效。