【问题标题】:UILabel not showing entire textUILabel 不显示整个文本
【发布时间】:2021-03-10 02:55:43
【问题描述】:

我有一个集合视图单元格,它有一个按钮和一个标签。因此,每当我在集合视图上添加项目时,都会显示多对按钮和标签,即 button1-label1,然后在 label1 和 button2 之间有一些空间后显示 button2-label2。

每当我为标签分配一些文本时,它只会显示标签上文本的前 2 个字母。我尝试增加标签的宽度,在标签上设置文本后调用'sizeToFit()'和'layoutIfNeeded()',但仍然没有变化。

我做错了什么?

标签框为 - (34.0, 10.0, 100.0, 16.0)

集合视图布局 API:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
    return 100
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
    return 100
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
    return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 25)
}

以下是标签设置

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    tech_human,你的 UI 是如何布局的,标签旁边的按钮是什么?

    如果是,则将按钮放在标签下方,并将尾随对齐约束设为Less than or Equal to 关系。

    【讨论】:

      【解决方案2】:

      我花了一些时间检查所有内容的 frame.width 值,并注意到即使标签的框架更大,实际集合视图单元格的 frame.width 小于标签的 frame.width。以下是解决方案:

      collectionViewCell.frame.size.width = collectionViewCell.label.frame.width + collectionViewCell.button.frame.width + 25
      

      上述解决方案中的 25 代表“minimumInteritemSpacingForSectionAt”,我将其从 100 更改为 25。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多