Multiline label in UIStackView

后端 未结 21 2920
有刺的猬
有刺的猬 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:12

    For a horizontal stack view that has a UILabel as one of its views, in Interface Builder firstly set label.numberOfLines = 0. This should allow the label to have more than 1 line. This initially failed to work for me when the stack view had stackView.alignment = .fill. To make it work simply set stackView.alignment = .center. The label can now expand to multiple lines within the UIStackView.

    The Apple documentation says

    For all alignments except the fill alignment, the stack view uses each arranged view’s intrinsic​Content​Size property when calculating its size perpendicular to the stack’s axis

    Note the word except here. When .fill is used the horizontal UIStackView does NOT resize itself vertically using the arranged subviews sizes.

提交回复
热议问题