简体   繁体   English

为什么我的 UILabel 不居中?

[英]Why isn't my UILabel centered?

I'm trying to center my UILabel horizontally.我试图将我的 UILabel 水平居中。 This is what it ends up looking like (notice its no center).这就是它最终的样子(注意它没有中心)。

在此处输入图像描述

    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];

The textAlignment property will align text within the frame of the UILabel , not outside of it. textAlignment属性将对齐UILabel框架内的文本,而不是框架外。 My ASCII skills are a little rusty, but here's what I mean.我的 ASCII 技能有点生疏,但这就是我的意思。 Consider the labels below, where the |考虑下面的标签,其中| indicates the left/right edge of the label.表示 label 的左/右边缘。

|173 lbs    | => left aligned
|    173 lbs| => right aligned
|  173 lbs  | => center aligned (not exactly centered in terms of its superview)

Now consider the same label contained within another view, where the outer [] represent the edges of the containing view, and the inner |现在考虑包含在另一个视图中的相同 label,其中外部[]表示包含视图的边缘,内部| represents the edges of the contained label. Notice how the inner label is not exactly centered within its superview, and is slightly pushed to the right (2 spaces away from the left, and 1 space closer to the right).表示包含的 label 的边缘。请注意内部 label 如何在其父视图中不完全居中,而是稍微向右推(距左侧 2 个空格,靠近右侧 1 个空格)。

[  |173 lbs    | ] => left aligned
[  |    173 lbs| ] => right aligned
[  |  173 lbs  | ] => center aligned

If you want the label to be center aligned within the superview or the screen at all times, you would want to make sure the width of the label matches its container, and then set textAlignment to center.如果您希望 label 在父视图或屏幕内始终居中对齐,您需要确保 label 的宽度与其容器匹配,然后将textAlignment设置为居中。 Here's how:这是如何做:

[|173 lbs      |] => left aligned
[|      173 lbs|] => right aligned
[|   173 lbs   |] => center aligned (perfectly aligned)

This is a common case where you want the subview to match the exact size of the superview.这是一种常见的情况,您希望子视图与父视图的确切大小相匹配。 You can use the bounds property to easily do that.您可以使用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];

As a UI debugging tip, try changing the background of the problematic view (label) to show exactly what area it is taking, which usually helps solving many such problems.作为 UI 调试提示,尝试更改有问题的视图(标签)的背景以准确显示它占用的区域,这通常有助于解决许多此类问题。

Just for debugging purposes, try setting the backgroundColor of aLabel and aReflectionView to see where they're being placed.仅出于调试目的,尝试设置aLabelaReflectionViewbackgroundColor以查看它们的放置位置。

aLabel.backgroundColor = [UIColor redColor];
aReflectionView.backgroundColor = [UIColor blueColor];

You should see that the aReflectionView is properly centered within its superview, but the aLabel , contained within it, is not.您应该看到aReflectionView在其父视图中正确居中,但包含在其中的aLabel是。 Why not?为什么不? Because of this line:因为这一行:

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

Since aLabel is contained within aReflectionView , it should be centered within the aReflectionView bounds, not the bounds of self.view .由于aLabel包含在aReflectionView中,它应该在aReflectionView范围内居中,而不是在self.view范围内。 Scroll right to see the change:向右滚动以查看更改:

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

For your purposes, it may be better to make aLabel simply fill aReflectionView :出于您的目的,最好让aLabel简单地填充aReflectionView

UILabel *aLabel = [[UILabel alloc]initWithFrame:aReflectionView.bounds];

The problem is not the alignment of the text in the UILabel, but rather the alignment of the UILabel itself.问题不是 UILabel 中文本的 alignment,而是 UILabel 本身的 alignment。 Specifically, you're creating the UILabel to be center aligned according to the width of the self.view (notably the width), but you're adding it to the aReflectionView (which is narrower, offset from the left edge of the self.view, and is already centered on the screen).具体来说,您正在创建 UILabel 以根据 self.view 的宽度(特别是宽度)居中对齐,但是您将其添加到 aReflectionView(较窄,从 self.view 的左边缘偏移)。视图,并且已经位于屏幕中心)。 The.net effect in this case, is that you're getting the offset from the left edge of the self.view twice, once in the x coordinate of the aReflectionView and then again in the x coordinate of the aLabel which you've put in the aReflectionView, which is clearly not your intent.在这种情况下,.net 效果是您从 self.view 的左边缘获得两次偏移量,一次在 aReflectionView 的 x 坐标中,然后再次在您放置的 aLabel 的 x 坐标中在 aReflectionView 中,这显然不是您的意图。 The easiest fix is probably something like:最简单的修复可能是这样的:

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

Bottom line, center the label on the view you're putting it on.最重要的是,将 label 置于您要放置的视图的中心。

For those Googling, I have an answer for them.对于那些谷歌搜索,我有一个答案。 Problem was, I had some escape characters to designate a new line within the code like so:问题是,我有一些转义字符来指定代码中的新行,如下所示:

self.emptyLabel.text = @"No likes yet! \n\
\
\n(Like some cards to save them here)";

That caused the text to be aligned off-center.这导致文本偏离中心对齐。 Simply remove them like so:像这样简单地删除它们:

self.emptyLabel.text = @"No likes yet! \n\n(Like some cards to save them here)";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM