如何缩小字体大小以适应堆栈视图但在整个堆栈视图中保持相同的字体大小?a

How to shrink font size to fit in the stackview but remain the same fontsizes in the whole stackview?a

如何缩小字体大小以适应 的 3 项内容?请务必注意,Stackview 中所有视图的字体大小应相同。

请看下面的问题。 “我的订单”选项卡的字体比 Stackview 的其余部分小。

我想达到什么目标?

使用相同的字体大小在 Stackview 中均匀分布所有项目。

我已经尝试了什么?

Setting the following properties on the labels.

label.adjustsFontSizeToFitWidth = true
label.minimumScaleFactor=0.75

Setting the following properties on the Stackview.

 stackView.axis = .horizontal
    stackView.distribution = .equalSpacing
    stackView.alignment = .center
    stackView.spacing = 16.0

感谢您的建议。

您尝试过使用 fillEqually 代替吗?这应该给你两个项目相同的大小。

我解决这个问题的方法是根据标签中的字符数计算每个项目应占堆栈视图总宽度的百分比,并使用该信息对标签设置宽度限制。像这样:

let totalCount = labels(into: 0) { [=10=] += .text.count }
labels.forEach { label in
    let widthRatio = CGFloat(label.text.count) / CGFloat(totalCount)
    label.widthAnchor.constraint(equalTo: label.superview!.widthAnchor, multiplier: widthRatio).isActive = true
}