0

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.

user3784622
  • 435
  • 1
  • 5
  • 15
  • Have you set the cell's content-hugging priorities? If not, open the size inspector and set them to 1000. – NRitH Jul 13 '14 at 04:27
  • That caused no difference, although that is probably because I had to subclass the UICollectionViewCell and add the label programmatically because the interface builder has a problem with labels in cells in Xcode 6 http://stackoverflow.com/questions/24492275/label-outlet-on-custom-uicollectionviewcell-in-swift-causing-optional-none-crash – user3784622 Jul 13 '14 at 04:49
  • `.sizeWithAttributes()` should be called with the attributes of the actual label text. – duci9y Jul 13 '14 at 08:08

1 Answers1

1

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

Essan Parto
  • 703
  • 11
  • 18