Multiline label in UIStackView

后端 未结 21 2761
有刺的猬
有刺的猬 2020-12-02 04:30

When putting multiline label (with linebreak set to Word Wrap) into a stack view, the label immediately loses the linebreak and displays the label text in one line instead.<

相关标签:
21条回答
  • 2020-12-02 05:14

    If you are create UI with only code Try it with this way

    e.g

    let label = UILabel()
    stackView.addArrangedSubview(label)        
    label.widthAnchor.constraint(equalTo: stackView.widthAnchor).isActive = true
    label.text = "blahblahblahblahblahblahblahblahblahblahblahblah"
    label.numberOfLines = 0
    label.lineBreakMode = .byWordWrapping
    label.layoutIfNeeded()
    
    0 讨论(0)
  • 2020-12-02 05:16

    For me, the issue was that the height of the stack view was simply too short. The label and stack view were properly set up to allow the label to have multiple lines, but the label was the first victim of content compression that the stack view used to get its height small enough.

    0 讨论(0)
  • 2020-12-02 05:17

    Add UIStackView properties,

    stackView.alignment = .fill
    stackView.distribution = .fillProportionally
    stackView.spacing = 8.0
    stackView.axis = .horizontal
    

    Instead of adding label inside UIView which is not required.If you are using inside UITableViewCell please, reload data on rotation.

    0 讨论(0)
  • 2020-12-02 05:18

    Here's a full example of a vertical UIStackView made up of multiline UILabels with automatic height.

    The labels wrap based on the stackview's width and the stackview's height is based on the label's wrapped height. (With this approach you don't need to embed the labels in a 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") }
    }
    

    Here is a sample ViewController that uses it.

    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])
        }
    }
    
    0 讨论(0)
  • 2020-12-02 05:19

    Just set number of lines to 0 in Attribute inspector for label. It will work for you.

    0 讨论(0)
  • 2020-12-02 05:20

    It's almost like @Andy's Answer, but you can add your UILabel in extra UIStackview, vertical worked for me.

    0 讨论(0)
提交回复
热议问题