繁体   English   中英

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

[英]Swift How to wrap long text of label in collection cell and set margin in text label

现在,我正在使用自定义集合视图布局来实现这一点。

在此输入图像描述

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

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
}

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

这是我正在使用的约束: 在此输入图像描述

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

顺便说一句,当我在控制台输出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 :) - re ct ==>(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)

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

plz设置宽度约束grater than或者等于 在此输入图像描述

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

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

label.numberOfLines = 0

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM