如何在 XCode 中居中标签文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3038767/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 19:27:20  来源:igfitidea点击:

How to center a label's text in XCode

iphonexcodelabel

提问by Linux world

I have a cell with two labels. How can I programmatically center-align the top label and hide the second label?

我有一个带有两个标签的单元格。如何以编程方式将顶部标签居中对齐并隐藏第二个标签?

回答by Alessandro Pirovano

UITextAlignmentCenteris deprecated. You should use the following:

UITextAlignmentCenter已弃用。您应该使用以下内容:

labelOne.textAlignment = NSTextAlignmentCenter;

回答by RickiG

[labelOne setTextAlignment:UITextAlignmentCenter]; //to center text in the UILabel [labelTwo setHidden:YES] //there, but not visible.

[labelOne setTextAlignment:UITextAlignmentCenter]; // 使 UILabel 中的文本居中 [labelTwo setHidden:YES] // 在那里,但不可见。

回答by 0yeoj

I think what you are trying to do is: Something like this..

我认为您正在尝试做的是:像这样的事情..

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    firstLabel.frame = cell.frame; // or reconfigure your frame or some reason(i dont know)
    firstLabel.textAlignment = NSTextAlignmentCenter;
    secondLabel.hidden = YES;
}

you want you text from the firstLabel to be at the center of the cell?

您希望 firstLabel 中的文本位于单元格的中心吗?