简体   繁体   English

如何在另一个 UIView 的同一中心位置制作标签的第一行

[英]how to make the first line of label in the same centre position of another UIView

在此处输入图片说明

I want to make the first line of those words (I am using UI Label here, or should I use Text View?), in the center of red line, the center of that red line is located at the center of that yellow UIView我想制作这些单词的第一行(我在这里使用UI Label,还是应该使用Text View?),在红线的中心,那条红线的中心位于那个黄色UIView的中心

I have tried some autolayout configuration but still failed to achieve this我尝试了一些自动布局配置,但仍然无法实现在此处输入图片说明

为了与多行标签的第一行对齐,您应该使用约束NSLayoutAttributeFirstBaseline (在 iOS8 中可用)或UIView firstBaselineAnchor上的锚点(在 iOS9 中可用)。

I solved this recently in a slightly less involved way by adding a hidden single line label in exactly the same location and font as the multiline one, without a bottom constraint.我最近通过在与多行标签完全相同的位置和字体中添加一个隐藏的单行标签而没有底部约束,以一种稍微不那么复杂的方式解决了这个问题

Then you can simply align the icon image .centerY to the hidden label's .centerY .然后您可以简单地将图标图像.centerY与隐藏标签的.centerY

self.view.addConstraint(
    NSLayoutConstraint(
        item: icon,
        attribute: .centerY,
        relatedBy: .equal,
        toItem: hiddenLabel,
        attribute: .centerY,
        multiplier: 1,
        constant: 0
    )
)

多行标签图标对齐

This is an involved process.这是一个复杂的过程。 Take your time to understand the whole logic.花点时间了解整个逻辑。

First of all we have some considerations:首先,我们有一些考虑:

  • The Yellow view has a defined height (as well as a defined width, so that it's horizontal center can be determined) Yellow视图具有定义的高度(以及定义的宽度,以便可以确定它的水平中心)
  • The label has a fixed font size (so that the horizontal center of first line can be aligned with the horizontal center of the Yellow view by means of constraint)标签有固定的字体大小(这样第一行的水平中心可以通过约束与黄色视图的水平中心对齐)

We will add a placeholder view to the right hand side of the Yellow view and make them center aligned.我们将在黄色视图的右侧添加一个占位符视图并使它们居中对齐。 Here comes the handy UIStackView .方便的UIStackView来了。

We take a UIStackView with these properties:我们使用具有以下属性的UIStackView

  • Axis: Horizontal轴: Horizontal
  • Alignment: Center对齐方式: Center
  • Distribution: Fill分布: Fill
  • Spacing: As your need间距:根据您的需要

The above stack view uses the Auto Layout:上面的堆栈视图使用了自动布局:

  • Leading, Trailing, Top: With respect to the super view (view controller's view / safe area)前导、尾随、顶部:关于超级视图(视图控制器的视图/安全区域)
  • Height: A fixed height (this will be the height of the Yellow view)高度:固定高度(这将是黄色视图的高度)

These are the constraints for the stack view:这些是堆栈视图的约束:

堆栈视图约束

Now we will add two views to the stack view with the attributes:现在我们将使用属性向堆栈视图添加两个视图:

  • Yellow view:黄色视图:
    • Width: A fixed width宽度:固定宽度
    • Height: Equal to the height of the stack view高度:等于堆栈视图的高度
    • But note that, I used a width & aspect ratio constraint so that the view remains rectangular但请注意,我使用了宽度和纵横比约束,以便视图保持矩形
  • Placeholder view:占位符视图:
    • Height: 0高度:0

These are the constraints for the two views:这些是两个视图的约束:

两个视图的约束

Now comes the label's part.现在是标签的部分。 It's a multiline label (number of lines set to 0 ) The label will be aligned with the placeholder view by top, leading & trailing constraints.这是一个多行标签(行数设置为0 )标签将通过顶部、前导和尾随约束与占位符视图对齐。 But the top constraints need a slight consideration.但是顶部的限制需要稍微考虑一下。 The label will have a font size of 20 so that if the top constraint has -13 (not -10 because there is a top padding between the label's frame and label's text) value with the placeholder, then it can be aligned with the horizontal center of the Yellow view.标签的字体大小为20因此如果顶部约束具有-13 (不是-10因为标签的框架和标签的文本之间存在顶部填充)值与占位符,那么它可以与水平中心对齐的黄色视图。

These are the constraints for the label:这些是标签的约束:

标签的约束


Enough of the talking.聊够了。 I have made a demo which you'll find here .我制作了一个演示,你可以在这里找到。 Also keep in mind if you need to control the horizontal space between the yellow view and the label, you can achieve that by changing the spacing value of the stack view.还要记住,如果您需要控制黄色视图和标签之间的水平空间,您可以通过更改堆栈视图的间距值来实现。

Also keep in mind that, this implementation is very tightly configured.还请记住,此实现的配置非常严格。 Anything changes, everything breaks.什么都变了,什么都坏了。 But if your need is slightly changed so that the first line of the label is vertically aligned (not the horizontal center of the first line) you can forget about the font size of the label and the -13 top constraint's value.但是,如果您的需要稍微改变,以便标签的第一行垂直对齐(而不是第一行的水平中心),您可以忘记标签的字体大小和-13顶部约束的值。


Below is the final output.下面是最终的输出。 I added a horizontal redline to indicate the middle of the yellow view.我添加了一条水平红线来指示黄色视图的中间。

在此处输入图片说明

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

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