首页 > 解决方案 > UILabel 不显示整个文本

问题描述

我有一个集合视图单元格,它有一个按钮和一个标签。因此,每当我在集合视图上添加项目时,都会显示多对按钮和标签,即 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)
}

以下是标签设置

在此处输入图像描述

在此处输入图像描述

标签: iosswift

解决方案


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

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

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


推荐阅读