为什么会发生这种情况,如何在堆叠视图中保留多行标签?
正确的答案在这里:
https://dev59.com/C1sW5IYBdhLWcg3w-rMA#43110590
UILabel
插入到UIView
中(Editor->嵌入->视图)UILabel
适配到UIView
上(例如,trailing space、top space和leading space至superview约束)UIStackView
会将UIView
拉伸以适当地适配界面,并且UIView
会将UILabel
限制为多行。
UIView
的花样操作。 - shimUILabel
作为其视图之一,在Interface Builder中首先设置label.numberOfLines = 0
。这应该允许标签有多行文本。当堆栈视图具有stackView.alignment = .fill
时,我最初试图使其工作失败了。为了使其工作,请简单设置stackView.alignment = .center
。现在可以通过UIStackView
展开多行文本。
Apple documentation说:
请注意此处的except词。当使用对于除填充对齐外的所有对齐方式,堆栈视图在计算垂直于堆栈轴的大小时使用每个已排序视图的intrinsicContentSize属性。
.fill
时,水平UIStackView
不会使用安排的子视图的大小来自动调整自身大小。.alignment
of the UIStackView that you have to set to anything but .fill
not the UILabel, which is a little confusing in the example given label.alignment = .center
. I think this should be stackView.alignment = .center
。你需要将UIStackView的.alignment
设置为除了.fill
以外的任何值,而不是UILabel,这在给出的示例中有点令人困惑,label.alignment = .center
。我认为应该是stackView.alignment = .center
。 - ae14UIViewController
的viewDidLayoutSubviews
方法中或者在继承了UIView
的子类的layoutSubviews
方法中指定标签的preferredMaxLayoutWidth
。 - Shyngys Kassymov尝试了以上所有建议后,我发现UIStackView不需要更改任何属性。我只需要按照以下方式更改UILabel的属性(标签已经添加到垂直堆栈视图中):
Swift 4 示例:
[titleLabel, subtitleLabel].forEach(){
$0.numberOfLines = 0
$0.lineBreakMode = .byWordWrapping
$0.setContentCompressionResistancePriority(UILayoutPriority.required, for: .vertical)
}
label.setContentCompressionResistancePriority(.fittingSizeLevel, for: .horizontal)
,它对我很有效。 - Vadim Kurochkin将preferredMaxLayoutWidth设置为UILabel对我起了作用
self.myLabel.preferredMaxLayoutWidth = self.bounds.size.width;
.center
,我真的不需要把标签放在 UIView 内部。 - Fariza Zhumat2020年11月26日,Xcode 12.2,iOS 14.2。
以下对于我来说适用于垂直堆栈视图。它在所有设备和模拟器上都有效。我使用storyboard,并且所有这些值都在storyboard中设置。
UILabel
行数:0
UIStackView
对齐方式:填充
分配:按比例填充
UILabel嵌入在一个视图中,并固定到UIView的所有边缘。
iOS 9+
在设置UILabel的文本后,调用[textLabel sizeToFit]
方法。
sizeToFit将使用preferredMaxWidth重新布局多行标签。该标签将调整stackView的大小,从而调整单元格的大小。除了将stackView固定到contentView之外,不需要任何其他约束条件。
UIStackView
示例,由多行的自动高度UILabel
组成。标签根据StackView的宽度换行,StackView的高度基于标签的换行高度。(使用这种方法,您不需要将标签嵌入到UIView
中。)(Swift 5,iOS 12.2)
// A vertical stackview with multiline labels and automatic height.
class ThreeLabelStackView: UIStackView {
let label1 = UILabel()
let label2 = UILabel()
let label3 = UILabel()
init() {
super.init(frame: .zero)
self.translatesAutoresizingMaskIntoConstraints = false
self.axis = .vertical
self.distribution = .fill
self.alignment = .fill
label1.numberOfLines = 0
label2.numberOfLines = 0
label3.numberOfLines = 0
label1.lineBreakMode = .byWordWrapping
label2.lineBreakMode = .byWordWrapping
label3.lineBreakMode = .byWordWrapping
self.addArrangedSubview(label1)
self.addArrangedSubview(label2)
self.addArrangedSubview(label3)
// (Add some test data, a little spacing, and the background color
// make the labels easier to see visually.)
self.spacing = 1
label1.backgroundColor = .orange
label2.backgroundColor = .orange
label3.backgroundColor = .orange
label1.text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi."
label2.text = "Hello darkness my old friend..."
label3.text = "When I wrote the following pages, or rather the bulk of them, I lived alone, in the woods, a mile from any neighbor, in a house which I had built myself, on the shore of Walden Pond, in Concord, Massachusetts, and earned my living by the labor of my hands only."
}
required init(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }
}
ViewController
。class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let myLabelStackView = ThreeLabelStackView()
self.view.addSubview(myLabelStackView)
// Set stackview width to its superview.
let widthConstraint = NSLayoutConstraint(item: myLabelStackView, attribute: NSLayoutConstraint.Attribute.width, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self.view, attribute: NSLayoutConstraint.Attribute.width, multiplier: 1, constant: 0)
self.view.addConstraints([widthConstraint])
}
}
UIStackView
设置widthAnchor
。
设置leadingAnchor
和trailingAnchor
不会起作用,但设置centerXAnchor
和widthAnchor
可以使UILabel正确显示。