Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 为什么我的UILabel不居中?_Iphone_Objective C_Ios_Cocoa Touch - Fatal编程技术网

Iphone 为什么我的UILabel不居中?

Iphone 为什么我的UILabel不居中?,iphone,objective-c,ios,cocoa-touch,Iphone,Objective C,Ios,Cocoa Touch,我正试图使我的UILabel水平居中。这就是它最后的样子,注意它没有中心 ReflectionView *aReflectionView = [[ReflectionView alloc]initWithFrame:CGRectMake(((self.view.bounds.size.width / 2) - 150), 40, 300, 90)]; UILabel *aLabel = [[UILabel alloc]initWithFrame:CGRectMake(((self

我正试图使我的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) 如果希望标签始终在superview或屏幕内居中对齐,则需要确保标签的宽度与其容器匹配,然后将textAlignment设置为居中。以下是方法:

[|173 lbs |] => left aligned [| 173 lbs|] => right aligned [| 173 lbs |] => center aligned (perfectly aligned) 作为UI调试技巧,请尝试更改有问题的视图标签的背景,以准确显示它所处的区域,这通常有助于解决许多此类问题。

textAlignment属性将在UILabel框架内对齐文本,而不是在UILabel框架外对齐文本。我的ASCII技能有点生疏,但我的意思是。考虑下面的标签,其中标签表示标签的左/右边缘。

|173 lbs | => left aligned | 173 lbs| => right aligned | 173 lbs | => center aligned (not exactly centered in terms of its superview) 如果希望标签始终在superview或屏幕内居中对齐,则需要确保标签的宽度与其容器匹配,然后将textAlignment设置为居中。以下是方法:

[|173 lbs |] => left aligned [| 173 lbs|] => right aligned [| 173 lbs |] => center aligned (perfectly aligned)
作为UI调试技巧,请尝试更改有问题视图标签的背景,以准确显示其所在区域,这通常有助于解决许多此类问题。

仅出于调试目的,请尝试设置aLabel和aReflectionView的背景色,以查看它们的放置位置

aLabel.backgroundColor = [UIColor redColor];
aReflectionView.backgroundColor = [UIColor blueColor];
您应该看到,aReflectionView在其superview中正确居中,但包含在其中的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:


仅出于调试目的,请尝试设置aLabel和aReflectionView的背景色,以查看它们的放置位置

aLabel.backgroundColor = [UIColor redColor];
aReflectionView.backgroundColor = [UIColor blueColor];
您应该看到,aReflectionView在其superview中正确居中,但包含在其中的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中文本的对齐方式,而在于UILabel本身的对齐方式。具体来说,您创建的UILabel根据self.view的宽度(尤其是宽度)居中对齐,但您将其添加到一个更窄的aReflectionView中,该视图偏离self.view的左边缘,并且已经在屏幕上居中。在这种情况下,最终的效果是,从self.view的左边缘获得两次偏移,一次在aReflectionView的x坐标中,然后在aReflectionView中放置的Alable的x坐标中,这显然不是您的意图。最简单的修复方法可能是:

UILabel *aLabel = [[UILabel alloc] initWithFrame:CGRectMake(((aReflectionView.bounds.size.width / 2) - 150), 0, 300, 90)];

底线是,将标签放在视图的中心。

问题不在于UILabel中文本的对齐,而在于UILabel本身的对齐。具体来说,您创建的UILabel根据self.view的宽度(尤其是宽度)居中对齐,但您将其添加到一个更窄的aReflectionView中,该视图偏离self.view的左边缘,并且已经在屏幕上居中。在这种情况下,最终的效果是,从self.view的左边缘获得两次偏移,一次在aReflectionView的x坐标中,然后在aReflectionView中放置的Alable的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)";

对于那些谷歌搜索,我有一个答案。问题是,我有一些转义字符来指定代码中的新行,如下所示:

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)";

看起来文本太大了。缩小它,看它是否有中心需要确认。看起来文本太大了。缩小它,看看是否能得到中心的确认。解释得很好。基本上更改UILabel*aLabel=[[UILabel alloc]initWithFrame:CGRectMakeself.view.bounds.size.width/2-150,0300,90];到UILabel*aLabel=[[UILabel alloc]initWithFrame:CGRectMake0,0300,90]@InsertWittyName-感谢您的提示,让我将其添加到
答案。解释得很好。基本上更改UILabel*aLabel=[[UILabel alloc]initWithFrame:CGRectMakeself.view.bounds.size.width/2-150,0300,90];到UILabel*aLabel=[[UILabel alloc]initWithFrame:CGRectMake0,0300,90]@InsertWittyName-谢谢你的提示,让我把它添加到答案中。