gpt4 book ai didi

ios - 如何在一行内垂直对齐两个 UILabel?

转载 作者:可可西里 更新时间:2023-11-01 00:54:16 25 4
gpt4 key购买 nike

enter image description here

我试图将两个 UILabel 放在一行中:一个 UILable 在左侧,文本左对齐,一个 UILabel在右侧,文本右对齐。请看上图。它们有不同的字体大小。

我使用了以下自动布局约束:

 let labelHorizontalConstrains = NSLayoutConstraint.constraints(
withVisualFormat: "H:|-20-[nameLabel]-15-[birthDeathDateLabel]-20-|",
metrics: nil,
views: views)
allConstraints += labelHorizontalConstrains

但结果显示它们不是垂直对齐的:左边的UILabel低于右边的UILabel。如何解决这个问题?

最佳答案

您需要添加一个centerYAnchor 约束。

birthDeathDateLabel.centerYAnchor.constraint(equalTo: nameLabel.centerYAnchor).isActive = true

但是,如果您希望它与文本的底部对齐,您应该使用 UIStackView

let stackView = UIStackView(arrangedSubviews: [nameLabel, birthDeathDateLabel])
stackView.alignment = .firstBaseline
stackView.axis = .horizontal
// Add other stackview properties and constraints

关于ios - 如何在一行内垂直对齐两个 UILabel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54385298/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com