如何使 UILabel 在 UIView 上居中
如何将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
怎么样:
How about:
如果标签是视图的子视图:
不要使用
view.center
,除非标签和视图具有相同的超级视图。如果没有直接连接,那么您必须将视图的中心转换为标签父级的坐标空间。然后你可以使用 PengOne 的答案和变换后的点。
If the label is a subview of view:
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.
如果您所拥有的是已经设置的标签并且您想要将其内容居中,请使用 NSTextAlignmentCenter。
Use NSTextAlignmentCenter if what you have is the label already set and you want to center its content.
更好的解决方案是:
第一:如果您以编程方式执行此操作,则需要使用框架和一些自定义进行初始化:
第二:如果标签是您想要的内容中心,您可以在调用
setNumberOflines
选择器并分配0 value
后运行此命令,您的文本将包含所需的所有行,并调用sizeToFit
方法以具有一个很好的定制,最后:这将
仅将 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:
Second: If a label what you are trying to center you can just run this after
setNumberOflines
selector called and0 value
assigned, your text will have all lines needed, andsizeToFit
method called to have a good customization, and finally:This will center
only the X axis
, and theY 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 butinterface builder
designed, just need to run thesecond code
.PengOne / Alex Lockwood 的答案简单又有用。如果您打算支持旋转,请添加以下内容以使其保持居中:
PengOne / Alex Lockwood's answer is simple and useful. If you intend to support rotation, add the following to keep it centered:
像这样的事情应该可以解决问题......
确保您的标签设置为居中对齐,并且足够大/宽以处理您的文本字符串。
我认为这应该满足您的要求。
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.
I think that should do what you are asking for.
如果您的 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];