简体   繁体   English

使用不同字体大小垂直对齐标签,Swift

[英]Vertically align labels with different font sizes, Swift

I would like to vertically align labels with different font sizes, so that they appear sitting on a common baseline. 我想垂直对齐具有不同字体大小的标签,以使它们看起来位于相同的基线上。

In this case, I have three labels embedded inside a stackview as you can see in the below picture. 在这种情况下,我在stackview中嵌入了三个标签,如下图所示。 Number 30 appears to be clearly offset from the baseline, whereas text 'In' and 'MINUTES' appears to be offset too, but not significant. 数字30似乎与基线明显偏离,而文字“ In”和“ MINUTES”也似乎偏离了基线,但并不明显。 How can I get all three to be sitting on the same baseline ? 我怎样才能使三个人都坐在同一基线上?

在此处输入图片说明

I am creating the labels and stack view programatically as you can see below: 我正在以编程方式创建标签和堆栈视图,如下所示:

var timeStackView           : UIStackView!
var timeIndication          : UILabel!
var time                    : UILabel!
var unitsOrTimeZone         : UILabel!

timeStackView = UIStackView()
timeStackView.addArrangedSubview(timeIndication)
timeStackView.addArrangedSubview(time)
timeStackView.addArrangedSubview(unitsOrTimeZone)
timeStackView.alignment = .bottom      // bottom alignment
timeStackView.axis = .horizontal
timeStackView.distribution = .equalSpacing
timeStackView.spacing = 10.0

All the three labels are created in a similar way as shown below, but with different text: 所有三个标签的创建方式如下所示,但文本不同:

timeIndication                      =   UILabel()
timeIndication.textAlignment        =   .center
timeIndication.text                 =   "In"
timeIndication.font                 =   timeIndication.font.withSize(14)
timeIndication.baselineAdjustment   = .alignBaselines // baselineAdjustment

I have tried timeStackView.alignment = .bottom and timeIndication.baselineAdjustment = .alignBaselines , but they don't seem to work. 我尝试过timeStackView.alignment = .bottomtimeIndication.baselineAdjustment = .alignBaselines ,但是它们似乎不起作用。

我没有尝试过,但是根据UIStackView文档,您应该将堆栈视图的对齐方式设置为.lastBaseline.firstBaseline ,具体取决于您希望如何处理多行标签。

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

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