为什么我的UILabel不居中?
我试图水平居中我的UILabel。这就是它最终的样子(注意它没有中心)。为什么我的UILabel不居中?
ReflectionView *aReflectionView = [[ReflectionView alloc]initWithFrame:CGRectMake(((self.view.bounds.size.width/2) - 150), 40, 300, 90)];
UILabel *aLabel = [[UILabel alloc]initWithFrame:CGRectMake(((self.view.bounds.size.width/2) - 150), 0, 300, 90)];
aLabel.textAlignment = UITextAlignmentCenter;
aLabel.text = @"1 lbs";
self.weightLabel = aLabel;
[aReflectionView addSubview:self.weightLabel];
self.weightReflectionView = aReflectionView;
[self.view addSubview:self.weightReflectionView ];
[self.weightReflectionView updateReflection];
的textAlignment
属性将所述UILabel
的框架内对齐文本,而不是外部的它。我的ASCII技能有点生疏,但这就是我的意思。考虑下面的标签,其中|
表示标签的左/右边缘。
|173 lbs | => left aligned | 173 lbs| => right aligned | 173 lbs | => center aligned (not exactly centered in terms of its superview)
现在考虑包含在另一视图,其中外[]
代表含视图的边缘内相同的标记,并且内|
表示包含标签的边缘。注意内部标签如何不在其超视图中精确居中,并稍微向右推(距左侧2个空间,靠近右侧1个空间)。
[ |173 lbs | ] => left aligned [ | 173 lbs| ] => right aligned [ | 173 lbs | ] => center aligned
如果您希望标签为中心,在任何时候上海华或屏幕内对齐,你会想,以确保标签的宽度它的容器相匹配,然后设置textAlignment
到中心。操作方法如下:
[|173 lbs |] => left aligned [| 173 lbs|] => right aligned [| 173 lbs |] => center aligned (perfectly aligned)
这是您希望子视图与超视图的确切大小相匹配的常见情况。你可以使用bounds
属性来轻松做到这一点。
ReflectionView *aReflectionView = [[ReflectionView alloc] initWithFrame:..];
// Use the bounds of the superview to set the subview's frame.
UILabel *aLabel = [[UILabel alloc] initWithFrame:aReflectionView.bounds];
作为UI调试尖,尝试改变问题的视图(标签)的背景显示什么区域正在采取,这通常有助于解决很多这样的问题。
很好解释。基本上改变UILabel * aLabel = [[UILabel alloc] initWithFrame:CGRectMake(((self.view.bounds.size.width/2) - 150),0,300,90)];到UILabel * aLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0,300,90)]; – InsertWittyName 2012-04-08 01:15:23
@InsertWittyName - 感谢您的提示,让我把它添加到答案。 – Anurag 2012-04-08 01:16:37
最后两行+1 :-) – 2013-09-20 11:47:21
仅用于调试目的,请尝试设置aLabel
和aReflectionView
的backgroundColor
以查看它们的放置位置。
aLabel.backgroundColor = [UIColor redColor];
aReflectionView.backgroundColor = [UIColor blueColor];
你应该看到aReflectionView
正确它的父内居中,但aLabel
,其中包含的,是不是。为什么不?由于该行的:
UILabel *aLabel = [[UILabel alloc]initWithFrame:CGRectMake(((self.view.bounds.size.width/2) - 150), 0, 300, 90)];
由于aLabel
包含内aReflectionView
,它应在aReflectionView
范围内居中,的self.view
不边界。向右滚动才能看到的变化:
UILabel *aLabel = [[UILabel alloc]initWithFrame:CGRectMake(((aReflectionView.bounds.size.width/2) - 150), 0, 300, 90)];
你的目的,它可能是更明智aLabel
只需填写aReflectionView
:
UILabel *aLabel = [[UILabel alloc]initWithFrame:aReflectionView.bounds];
的问题是不是在的UILabel的文本的对齐方式,但而不是UILabel本身的对齐。具体来说,你创建的UILabel要根据self.view的宽度(特别是宽度)居中对齐,但是你将它添加到aReflectionView(它比自己的左边缘偏离更窄)。视图,并且已经在屏幕上居中)。在这种情况下的净效应是,你得到了自我左边缘的偏移量。查看两次,一次在aReflectionView的x坐标中,然后再次放入aReablectionView中的aLabel的x坐标中,这显然不是您的意图。最简单的解决方案可能是这样的:
UILabel *aLabel = [[UILabel alloc] initWithFrame:CGRectMake(((aReflectionView.bounds.size.width/2) - 150), 0, 300, 90)];
底线,将标签放在您要放置的视图上。
对于那些谷歌搜索,我有一个答案。问题是,我有一些转义字符的代码内指定一个新行,像这样:
self.emptyLabel.text = @"No likes yet! \n\
\
\n(Like some cards to save them here)";
导致文本偏离中心对齐。只需将它们移除,如下所示:
self.emptyLabel.text = @"No likes yet! \n\n(Like some cards to save them here)";
看起来像文字太大。收缩它,看看它是否得到中心确认。 – wbyoung 2012-04-08 01:00:07