【问题标题】:How to get UIlabel to automatically span multiple lines dynamically. Inside a Tableview cell / ContentView with multiple UIlabels如何让 UIlabel 动态地自动跨越多行。在具有多个 UIlabels 的 Tableview 单元格/ContentView 内
【发布时间】:2019-11-16 15:19:59
【问题描述】:

我们在 Xcode 中使用 IB 有以下 UI。本质上是一个 TableView。 TableView 在 Content View 中有四行 - 这些不是表格行。

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

我们希望第 3 行中的 UILabel (CategoryDe​​tailLabel) 根据内容动态扩展。因此,根据此处的文档,我们将 TabelView 上的“行高”和“估计高度”打开为自动的。

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

当我们将 tableview 上的行高更改为 150 时,我们会显示所有行

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

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

【问题讨论】:

标签: ios uitableview uikit interface-builder


【解决方案1】:

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

2 - 将此代码添加到您的表格视图中。

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

3 - 不要为行使用高度。

【讨论】:

  • 并且一定要将标签的numberOfLines属性设置为0,以便动态调整显示的文本数量。
猜你喜欢
  • 1970-01-01
  • 2023-04-09
  • 2015-06-11
  • 2021-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-22
相关资源
最近更新 更多