ios 如何在 UIView 上居中 UILabel

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

How to center a UILabel on UIView

iosuiviewuilabel

提问by john doe

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

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

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

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

回答by PengOne

How about:

怎么样:

[self.label setCenter:view.center];

回答by Nashenas

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.centerunless the label and view have the same superview.

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

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.

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

回答by Alex Delgado

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

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

cell.menuLabel.textAlignment = NSTextAlignmentCenter;

回答by Zindokar

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 setNumberOflinesselector called and 0 valueassigned, your text will have all lines needed, and sizeToFitmethod called to have a good customization, and finally:

第二:如果标签你想要居中,你可以在setNumberOflines调用和0 value分配选择器后运行它,你的文本将包含所有需要的行,并sizeToFit调用方法以进行良好的自定义,最后:

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

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

这将居中only the X axis,并且Y axis将在框架初始化中保持您想要的状态。

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

PS:如果不是,它也有效,UILabel但取决于您使用的控件需要另一个简单的自定义或两者都不需要,如果您只想以编程方式居中但经过interface builder设计,只需运行second code.

回答by Nijar

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

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

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

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

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

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

回答by DenVog

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

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

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

回答by Richard Baxter

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.

我认为这应该做你所要求的。