如何使 UILabel 在 UIView 上居中

发布于 2024-11-25 00:36:08 字数 229 浏览 2 评论 0原文

如何将 UILabel 置于 UIView 的中心?我正在使用以下代码

float width = weatherView.bounds.size.width; 
float height = weatherView.bounds.size.height; 

[self.label setFrame:CGRectMake(width-100,height-100, 100, 100)];

How can I center the UILabel on the UIView? I am using the following code

float width = weatherView.bounds.size.width; 
float height = weatherView.bounds.size.height; 

[self.label setFrame:CGRectMake(width-100,height-100, 100, 100)];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

饮惑 2024-12-02 00:36:08

怎么样:

[self.label setCenter:view.center];

How about:

[self.label setCenter:view.center];
明月夜 2024-12-02 00:36:08

如果标签是视图的子视图:

[self.label setCenter:CGPointMake(view.frame.size.width / 2, view.frame.size.height / 2)]

不要使用view.center,除非标签和视图具有相同的超级视图。

如果没有直接连接,那么您必须将视图的中心转换为标签父级的坐标空间。然后你可以使用 PengOne 的答案和变换后的点。

If the label is a subview of view:

[self.label setCenter:CGPointMake(view.frame.size.width / 2, view.frame.size.height / 2)]

Don't use view.center unless the label and view have the same superview.

If there is no immediate connection, then you'll have to transform view's center to the coordinate space of label's parent. Then you could use PengOne's answer with the transformed point.

静待花开 2024-12-02 00:36:08

如果您所拥有的是已经设置的标签并且您想要将其内容居中,请使用 NSTextAlignmentCenter。

cell.menuLabel.textAlignment = NSTextAlignmentCenter;

Use NSTextAlignmentCenter if what you have is the label already set and you want to center its content.

cell.menuLabel.textAlignment = NSTextAlignmentCenter;
美煞众生 2024-12-02 00:36:08

更好的解决方案是:

第一:如果您以编程方式执行此操作,则需要使用框架和一些自定义进行初始化:

// Simple example
int yPosition = 10;
UILabel *label = [[UILabel alloc] initWithFrame: CGRectMake(0, yPosition, self.view.frame.size.width, 0)];
[label setText: @"Testing..."];
[label setBackgroundColor: [UIColor clearColor]];
[label setNumberOfLines: 0];
[label sizeToFit];

第二:如果标签是您想要的内容中心,您可以在调用 setNumberOflines 选择器并分配 0 value 后运行此命令,您的文本将包含所需的所有行,并调用 sizeToFit 方法以具有一个很好的定制,最后:

[self.label setCenter: CGPointMake(self.view.center.x, self.label.center.y)];

这将仅将 X 轴居中Y 轴将在框架初始化中按照您的需要保持不变。

PS:如果不是 UILabel 也有效,但取决于您使用的控件将需要另一个简单的自定义或两者都不需要,并且如果您只想以编程方式居中但 界面生成器设计,只需要运行第二个代码

A better posible solution is:

First: If you are doing this programmatically you'll need to initialize with frame and some customizations:

// Simple example
int yPosition = 10;
UILabel *label = [[UILabel alloc] initWithFrame: CGRectMake(0, yPosition, self.view.frame.size.width, 0)];
[label setText: @"Testing..."];
[label setBackgroundColor: [UIColor clearColor]];
[label setNumberOfLines: 0];
[label sizeToFit];

Second: If a label what you are trying to center you can just run this after setNumberOflines selector called and 0 value assigned, your text will have all lines needed, and sizeToFit method called to have a good customization, and finally:

[self.label setCenter: CGPointMake(self.view.center.x, self.label.center.y)];

This will center only the X axis, and the Y axis will stay as you desired in the frame initialization.

PS: It's also valid if not a UILabel but depends on the control you are using will need another simple customization or neither, and if you only want to center programmatically but interface builder designed, just need to run the second code.

把回忆走一遍 2024-12-02 00:36:08

PengOne / Alex Lockwood 的答案简单又有用。如果您打算支持旋转,请添加以下内容以使其保持居中:

[self.myLabel setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleWidth];

PengOne / Alex Lockwood's answer is simple and useful. If you intend to support rotation, add the following to keep it centered:

[self.myLabel setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleWidth];
不再让梦枯萎 2024-12-02 00:36:08

像这样的事情应该可以解决问题......
确保您的标签设置为居中对齐,并且足够大/宽以处理您的文本字符串。

float viewWidth = weatherView.frame.size.width;
float viewHeight = weatherView.frame.size.height;
float labelWidth = label.frame.size.width;
float labelHeight = label.frame.size.height;

float xpos = (viewWidth/2.0f) - (labelWidth/2.0f);
float ypos = (viewHeight/2.0f) - (labelHeight/2.0f);

[label setFrame:CGRectMake(xpos,ypos,labelWidth,labelHeight)];

我认为这应该满足您的要求。

Something like this should do the trick...
Make sure that your label is set to have centered alignment and is sufficiently big/wide to handle your text string.

float viewWidth = weatherView.frame.size.width;
float viewHeight = weatherView.frame.size.height;
float labelWidth = label.frame.size.width;
float labelHeight = label.frame.size.height;

float xpos = (viewWidth/2.0f) - (labelWidth/2.0f);
float ypos = (viewHeight/2.0f) - (labelHeight/2.0f);

[label setFrame:CGRectMake(xpos,ypos,labelWidth,labelHeight)];

I think that should do what you are asking for.

淡忘如思 2024-12-02 00:36:08

如果您的 UILabel 是通过 .xib 添加的,或者以编程方式添加的,则有两种可能性。

如果第一种情况即通过 .xib 添加,那么您可以使用“Arrange”属性从 xib 文件大小检查器选项卡设置位置

如果第二种情况持续存在,那么您可以设置为 --- [self.label setCenter:view.center];

There are two possibility if your UILabel is added via .xib or else added programmatically .

If first case i.e added via .xib then you can set the position from xib file size inspector tab with the 'Arrange' property

And if second case persist then you can set as --- [self.label setCenter:view.center];

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文