[英]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:首先,我们有一些考虑:
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
:
Horizontal
轴: Horizontal
Center
对齐方式: Center
Fill
分布: Fill
The above stack view uses the Auto Layout:上面的堆栈视图使用了自动布局:
These are the constraints for the stack view:这些是堆栈视图的约束:
Now we will add two views to the stack view with the attributes:现在我们将使用属性向堆栈视图添加两个视图:
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.