为什么我不能根据里面标签的长度来改变我的UICollectionViewCell的宽度?

时间:2017-01-18 00:46:14

标签: ios swift uicollectionview uicollectionviewcell uicollectionviewlayout

在我的Swift应用中,我正在使用UICollectionView。每个UICollectionViewCell都包含一个UILabel,在某些时候我会使用不同的字符串填充这些标签。但是,字符串有不同的长度,所以我想用正确的大小显示它们。目前我正在使用这两种方法:

   private func estimateFrameForText(text: String) -> CGRect {
        //we make the width arbitrarily large so we don't undershoot height in calculation
        let width: CGFloat = 200

        let size = CGSize(width: width, height: 33)
        let options = NSStringDrawingOptions.usesFontLeading.union(.usesLineFragmentOrigin)
        let attributes = [NSFontAttributeName: font]

        return NSString(string: text).boundingRect(with: size, options: options, attributes: attributes, context: nil)
    }

    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        var width: CGFloat = 200

        //we are just measuring width so we add a padding constant to give the label some room to breathe!
        var padding: CGFloat = 1

        //estimate each cell's width
        if let text = self.suggestedHashtags[indexPath.item] as? String {
            width = estimateFrameForText(text: text).width + padding
        }
        return CGSize(width: width, height: 35)
    }

但它不起作用,细胞大小仍然相同。 我错过了什么?

2 个答案:

答案 0 :(得分:3)

我认为您可以更改UICollectionViewCell的宽度。

在sizeForItemAtIndexPath方法中,您可以通过以下代码设置UICollectionViewCell的大小:

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
{
    var size = CGSizeZero
    if let text = self.suggestedHashtags[indexPath.item] as? String {
        size = text.size(attributes: [NSFontAttributeName: UIFont.systemFont(ofSize: 18.0)])
        return size
    }
    return size
}

答案 1 :(得分:2)

使用UICollectionViewDelegateFlowLayout委托方法。

class CONTROLLERNAME: UIViewController ,UICollectionViewDelegate , UICollectionViewDataSource , UICollectionViewDelegateFlowLayout{

 var items = ["12", "2", "3221", "434", "52342","5646445646454464654646464", "bdvjsd", "adscfaaaw", "How are you?"];


func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
       return CGSize(width: self.findHeightWidthForText(self.items[indexPath.item], havingWidth: self.view.frame.size.width - 18, andFont: UIFont.systemFontOfSize(17.0)).width + 12, height: 36) // here pass your font size and ya give extra some space for cell.
}


// method for find hight and width for particular string.
func findHeightWidthForText(text: String, havingWidth widthValue: CGFloat, andFont font: UIFont) -> CGSize {
        var size = CGSizeZero
        if text.isEmpty == false {
            let frame = text.boundingRectWithSize(CGSizeMake(widthValue, CGFloat.max), options: .UsesLineFragmentOrigin, attributes: [NSFontAttributeName: font], context: nil)
            size = CGSizeMake(frame.size.width, ceil(frame.size.height))
        }
        return size
    }

输出

Output