Answers:
正确的答案在这里:https :
//stackoverflow.com/a/43110590/566360
UILabel内部UIView(编辑器->嵌入->视图)UILabel于UIView(例如,后空间,顶部空间,并导致空间上海华约束)将UIStackView要伸出的UIView配合不当,而UIView将限制UILabel为多行。
UIView必要的恶作剧。
对于具有UILabel作为其视图之一的水平堆栈视图,在Interface Builder中首先设置label.numberOfLines = 0。这应该允许标签多于1行。最初,当堆栈视图出现时,这对我不起作用stackView.alignment = .fill。为了使其工作简单设置stackView.alignment = .center。标签现在可以扩展到中的多行UIStackView。
在苹果的文档说
对于除填充对齐方式之外的所有对齐方式,堆栈视图在计算垂直于堆栈轴的尺寸时会使用每个排列的视图的固有Content_Size属性
请注意除此处以外的单词。当.fill使用横向UIStackView不调整大小本身垂直使用布置子视图的尺寸。
.alignment该UIStackView的,你必须设置为任何东西,但.fill 不是在的UILabel,这是一个小中给出的例子混淆label.alignment = .center。我认为应该是stackView.alignment = .center
multiLine除非您将其设置为固定宽度,否则堆栈视图仍不会增长。当我们固定它的宽度时,当达到该宽度时,它会折断成多行,如下所示:如果我们不给栈视图固定的宽度,那么事情就会变得模棱两可。堆栈视图随标签一起增长多长时间(如果标签值为动态)?
希望这可以解决您的问题。
preferredMaxLayoutWidth在@AnandKumar中指定viewDidLayoutSubviews标签,UIViewController也可以在layoutSubviews子类中指定@ UIView。
将preferredMaxLayoutWidth设置为UILabel对我有用
self.myLabel.preferredMaxLayoutWidth = self.bounds.size.width;
.center,我真的不需要UIView内的标签。
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属性,
stackView.alignment = .fill
stackView.distribution = .fillProportionally
stackView.spacing = 8.0
stackView.axis = .horizontal
而不是在UIView不需要的标签内添加标签。如果要在标签内使用标签,UITableViewCell请在旋转时重新加载数据。
以下是多行标签在Playground中的实现,其中带有换行符UIStackView。它不需要在UILabel内部嵌入任何东西,并且已经通过Xcode 9.2和Swift 4进行了测试。希望它会有所帮助。
import UIKit
import PlaygroundSupport
let containerView = UIView()
containerView.frame = CGRect.init(x: 0, y: 0, width: 400, height: 500)
containerView.backgroundColor = UIColor.white
var label = UILabel.init()
label.textColor = .black
label.numberOfLines = 0
label.translatesAutoresizingMaskIntoConstraints = false
label.text = "This is an example of sample text that goes on for a long time. This is an example of sample text that goes on for a long time."
let stackView = UIStackView.init(arrangedSubviews: [label])
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.axis = .vertical
stackView.distribution = .fill
stackView.alignment = .fill
containerView.addSubview(stackView)
stackView.centerXAnchor.constraint(equalTo: containerView.centerXAnchor).isActive = true
stackView.centerYAnchor.constraint(equalTo: containerView.centerYAnchor).isActive = true
stackView.widthAnchor.constraint(equalTo: containerView.widthAnchor).isActive = true
stackView.heightAnchor.constraint(equalTo: containerView.heightAnchor).isActive = true
PlaygroundPage.current.liveView = containerView
系统布局应该弄清楚绘制子视图的原点,宽度和高度,在这种情况下,所有子视图都具有相同的优先级,这会产生冲突,布局系统不知道视图之间的依赖关系,哪个依赖关系首先绘制,然后依此类推
设置堆栈子视图压缩将解决多行问题,这取决于您的堆栈视图是水平视图还是垂直视图,以及要变为多行的视图。 stackOtherSubviews .setContentCompressionResistancePriority(.defaultHight, for: .horizontal)
lblTitle.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
什么对我有用!
stackview:对齐方式:填充,分布:填充,约束到superview ex的比例宽度。0.8,
标签:中心,线= 0
对于仍然无法使它起作用的任何人。尝试在该UILabel上使用最小字体比例设置自动收缩。
这几乎就像@ Andy's Answer,但您可以为您添加UILabel额外的UIStackview,垂直的作品。