如何让 UIlabel 动态地自动跨越多行。在具有多个 UIlabels 的 Tableview 单元格/ContentView 中

How to get UIlabel to automatically span multiple lines dynamically. Inside a Tableview cell / ContentView with multiple UIlabels

我们使用 IB 在 Xcode 中有以下 UI。本质上有一个TableView。 TableView 在内容视图中有四行 - 这些不是 table 行。

-- TableView
    -- StackView
        -- DateLabel (Row 1)
        -- StackView - Time and Category (Row 2)
              -- Timelabel
              -- Category label
        -- CategoryDetailLabel (Row 3)
        -- MoreLabel (Row 4)

我们希望第 3 行中的 UI 标签 (CategoryDe​​tailLabel) 根据内容动态展开。因此,根据此处的文档,我们将 TabelView 上的 "Row Height" 和 "Estimated Height" 设为自动。

但不幸的是只显示一行(行)

当我们将 table 视图上的行高更改为 150 时,我们将显示所有行

但是CategoryDe​​taillabel 并没有扩展它的高度。内容被截断。

我们已将 CategoryDe​​tailLabel 的 "Lines" 设置为 0。我们如何使用 IB 解决此问题。

1 - 将 UILabel 约束到单元格的所有边缘。

2 - 将此代码添加到您的 table 视图。

tableView.rowHeight = UITableView.automaticDimension
tableView.estimatedRowHeight = 44

3 - 不要为行使用高度。