简体   繁体   中英

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. Number 30 appears to be clearly offset from the baseline, whereas text 'In' and 'MINUTES' appears to be offset too, but not significant. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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