简体   繁体   English

Swift - 如何将 UILabel 上的文本垂直居中?

[英]Swift - How to vertically center the text on UILabel?

I have a horizontal stack view with 3 labels on it.我有一个水平堆栈视图,上面有 3 个标签。 Second label ("Separator Label" in the image) just has a 'dot' as the text, but since the label is within stack view, it takes the height of the stack view and other labels.第二个 label (图像中的“分隔符标签”)只有一个“点”作为文本,但由于 label 在堆栈视图内,它占用堆栈视图和其他标签的高度。 I want the dot to be in center, but it sits at the bottom.我希望点位于中心,但它位于底部。 I tried changing base alignment to center and view's content mode to center, but it still remains at the bottom.我尝试将基础 alignment 更改为居中并将视图的内容模式更改为居中,但它仍然位于底部。

How can I make the text / dot move to the center?如何使文本/点移动到中心? All I want is the dot vertically centered.我想要的只是垂直居中的点。

Below are the images - first image is the stack view and labels in File View, second image is contents of stack view and 3rd image is settings on the separator label.下面是图像 - 第一个图像是堆栈视图和文件视图中的标签,第二个图像是堆栈视图的内容,第三个图像是分隔符 label 上的设置。

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

If you want it centered vertically, you probably need to replace the label with an image.如果您希望它垂直居中,您可能需要将 label 替换为图像。 When you use text, the character space is centered the way you want but the font defines the period as being at the bottom of that space.当您使用文本时,字符空间以您想要的方式居中,但字体将句点定义为位于该空间的底部。

You could use a normal UIView instead of the UIStackView and add the subviews and constraint the "."您可以使用普通的UIView而不是UIStackView并添加子视图并约束“。” period UILabel to the centerX and it'll stay at the center forever irrespective of the other contents of the super view.期间UILabelcenterX并且它将永远停留在中心,而与超级视图的其他内容无关。

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

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