user3784622
user3784622

Reputation: 435

How to resize cell in UICollectionView based on UILabel subview inside of it

What I have done is set the label to resize using label.sizeToFit() which is convenient, however I now need to set the cell to properly adjust its size too.

I used the sizeForIndexPath method from the FlowLayoutDelegate to set the cell size to:

messages[indexPath.row].sizeWithAttributes(nil)

however that gives me very thin/small CGSize's.

import UIKit

let reuseIdentifier = "Cell"

class PhotoCollectionViewController: UICollectionViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
    var messages = NSString[]()

    override func viewDidLoad() {
        super.viewDidLoad()
        messages = ["Hello, How are you?", "Ldsfsd sdf dsfs s fs fs", "fsdfsdfsdfs fsdfsdfsdf sfs fs", "yoyo yoy oyo", "sdfd sfds fssfs"]

        self.collectionView.registerClass(TextCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        self.collectionView.dataSource = self
        self.collectionView.delegate = self
        self.collectionView.collectionViewLayout = PhotoCollectionViewFlowLayout()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    // #pragma mark UICollectionViewDataSource

    override func numberOfSectionsInCollectionView(collectionView: UICollectionView?) -> Int {
        return 1
    }


    override func collectionView(collectionView: UICollectionView?, numberOfItemsInSection section: Int) -> Int {
        //#warning Incomplete method implementation -- Return the number of items in the section
        return messages.count
    }

    override func collectionView(collectionView: UICollectionView?, cellForItemAtIndexPath indexPath: NSIndexPath?) -> UICollectionViewCell? {
        let cell = collectionView?.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as TextCollectionViewCell
        cell.backgroundColor = UIColor.lightGrayColor()
        cell.newLabel.text = messages[indexPath!.row]
        cell.newLabel.sizeToFit()

        return cell
    }

    func collectionView(collectionView: UICollectionView!, layout collectionViewLayout: UICollectionViewLayout!, sizeForItemAtIndexPath indexPath: NSIndexPath!) -> CGSize {
        //println(messages[indexPath.row].sizeWithAttributes(nil))
        return messages[indexPath.row].sizeWithAttributes(nil)
    }
}

Edit: I have got it to work, sorta (in an unoptimized fashion):

func collectionView(collectionView: UICollectionView!, layout collectionViewLayout: UICollectionViewLayout!, sizeForItemAtIndexPath indexPath: NSIndexPath!) -> CGSize {
    let cell = TextCollectionViewCell(frame: CGRect(x: 0, y: 8, width: 300, height: 100))
    cell.newLabel.text = messages[indexPath.item]
    cell.newLabel.sizeToFit()

    println(cell.newLabel.intrinsicContentSize())
    //var width = UIWindow.
    return CGSizeMake(cell.newLabel.intrinsicContentSize().width+10, cell.newLabel.intrinsicContentSize().height+20)
    //return cell.newLabel.intrinsicContentSize()
}

Now the text fits in most cases however: It does not fit when it is longer in width than the screen size as obviously a new line is not automatically started. It also is displayed in the middle of the screen, not sure where in the layout I can change this or whether it is dependent on something else.

Upvotes: 0

Views: 5112

Answers (1)

Essan Parto
Essan Parto

Reputation: 705

If you want to use self-sizing cells with UICollectionViewFlowLayout you need to set estimatedItemSize to a non-CGSizeZero size.

Upvotes: 1

Related Questions