Swift如何在集合单元格中包装标签的长文本并在文本标签中设置边距

时间:2016-06-24 05:36:09

标签: ios iphone xcode swift

目前,我正在使用自定义集合视图布局来实现此目的。

enter image description here

每个单元格都包含一个文本长度不同的标签。我使用下面的函数返回单元格的宽度:

func collectionView(collectionView: UICollectionView, widthForSubsectionAtIndexPath indexPath: NSIndexPath, height: CGFloat) -> CGFloat {
    let text = subsections[indexPath.row]
    let font = UIFont(name: "Helvetica neue", size: 24)
    dLog("height ==> \(height)")
    let rect = NSString(string: text).boundingRectWithSize(CGSize(width: CGFloat(MAXFLOAT), height: height), options: .UsesLineFragmentOrigin, attributes: [NSFontAttributeName: font!], context: nil)
    dLog("rect ==> \(rect)")
    return rect.width
}

这可以如图所示包装文本。但我也想让文字之间的距离相同。上面的红色圆圈尺寸不同。我想这取决于文本的长度。

以下是我使用的约束: enter image description here

如何设置文本和单元格之间的边距,同时正确包装单词?

BTW,当我在控制台中输出heightrect信息时,我发现高度信息不同。灰条的高度实际上是50.这是输出:

  

[SubsectionHeaderCollectionViewController.swift:91] collectionView(:widthForSubsectionAtIndexPath:height :) - height ==> 50.0   2016-06-24 12:13:50.507 Atlas [12714:496386] [SubsectionHeaderCollectionViewController.swift:93] collectionView(:widthForSubsectionAtIndexPath:height :) - rect ==> (0.0,0.0,63.192,27.96)   2016-06-24 12:13:50.507 Atlas [12714:496386] [SubsectionHeaderCollectionViewController.swift:91] collectionView(:widthForSubsectionAtIndexPath:height :) - height ==> 50.0   2016-06-24 12:13:50.507 Atlas [12714:496386] [SubsectionHeaderCollectionViewController.swift:93] collectionView(:widthForSubsectionAtIndexPath:height :) - rect ==> (0.0,0.0,60.432,27.96)   2016-06-24 12:13:50.507 Atlas [12714:496386] [SubsectionHeaderCollectionViewController.swift:91] collectionView(:widthForSubsectionAtIndexPath:height :) - height ==> 50.0   2016-06-24 12:13:50.508 Atlas [12714:496386] [SubsectionHeaderCollectionViewController.swift:93] collectionView(:widthForSubsectionAtIndexPath:height :) - rect ==> (0.0,0.0,241.8,27.96)   2016-06-24 12:13:50.508 Atlas [12714:496386] [SubsectionHeaderCollectionViewController.swift:91] collectionView(:widthForSubsectionAtIndexPath:height :) - height ==> 50.0   2016-06-24 12:13:50.508 Atlas [12714:496386] [SubsectionHeaderCollectionViewController.swift:93] collectionView(:widthForSubsectionAtIndexPath:height :) - rect ==> (0.0,0.0,98.232,27.96)   2016-06-24 12:13:50.508 Atlas [12714:496386] [SubsectionHeaderCollectionViewController.swift:91] collectionView(:widthForSubsectionAtIndexPath:height :) - height ==> 50.0   2016-06-24 12:13:50.508 Atlas [12714:496386] [SubsectionHeaderCollectionViewController.swift:93] collectionView(:widthForSubsectionAtIndexPath:height :) - rect ==> (0.0,0.0,56.88,27.96)

3 个答案:

答案 0 :(得分:0)

删除相等宽度,相等高度约束并将标签前导和尾随空格添加到单元格(constan 10),并将约束高度设置为单元格高度,然后根据文本中的文本动态设置单元格宽度标签。在设置宽度后,在cellForIndexMethod中添加此函数cell.layoutIfNeeded()。

答案 1 :(得分:0)

plz设置宽度约束grater than或等于enter image description here

此标签宽度设置为其文本。希望它的帮助

答案 2 :(得分:0)

在自定义collectionView单元对象中,将标签的numberOfLines属性设置为零。

label.numberOfLines = 0

然后在您的约束中,将标签固定在自定义collectionView单元的前缘和后缘。

yourLabel.leadingAnchor.constraint(equalTo: leadingAnchor),
yourLabel.trailingAnchor.constraint(equalTo: trailingAnchor)