1

I want dynamic cell Size as per text in TextView, Here is my code

        func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
        {
            return DataArray.count
        }
        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
        {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as? CustomCollectionViewCell

            let fixedWidth = cell?.TextView.frame.size.width
            cell?.TextView.sizeThatFits(CGSize(width: fixedWidth!, height: CGFloat.greatestFiniteMagnitude))
            let newSize = cell?.TextView.sizeThatFits(CGSize(width: fixedWidth!, height: CGFloat.greatestFiniteMagnitude))
            var newFrame = cell?.TextView.frame
            newFrame?.size = CGSize(width: max((newSize?.width)!, fixedWidth!), height: (newSize?.height)!)
            cell?.TextView.frame = newFrame!
            TextViewFrame = newFrame!
            cell?.TextView.text = DataArray[indexPath.row] as? String
            return cell!
        }
        func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
        {

            return CGSize(width: CV.frame.size.width, height: 200)
        }

Here i have given 200 fixed size for cell. it gives me output as below It gives me cell height 200 for small content as well as for large content . But actually i want cell height as per content. How can i achieve this? Any help is appreciated. Thanks in advance enter image description here

Fattie
  • 27,874
  • 70
  • 431
  • 719
Neelam Pursnani
  • 246
  • 3
  • 17
  • is there a maximum limit on the length of the string that you will be showing in the label? – Adeel Miraj Oct 10 '16 at 11:45
  • No, i did not apply any max limit – Neelam Pursnani Oct 10 '16 at 11:50
  • And what about the minimum height of a cell? Is there a chance that the string for some cell might be empty? – Adeel Miraj Oct 10 '16 at 11:57
  • @NeelamPursnani, it is still (2016) incredibly difficult to do this - it is just not easy. Prepare for weeks of work to become good at this, you could start with any of the huge number of articles on it example... http://stackoverflow.com/questions/18746929/ – Fattie Oct 10 '16 at 12:27
  • No string of cell can not be empty. i am trying to create chat window – Neelam Pursnani Oct 10 '16 at 12:30
  • As far as finding the height of the string is concerned the code that I have posted in my answer would do for you. If you are looking for something else then you can edit your question and provide more detail. – Adeel Miraj Oct 10 '16 at 12:46

1 Answers1

1

You can find the height of the cell once you know the bounding rect for the text you want to display.

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {

    let text = "String that you want to show in the label inside the cell"
    let boundingRect = NSString(string: text).boundingRectWithSize(CGSizeMake(collectionView.bounds.width, 1000),
                                                                   options: .UsesLineFragmentOrigin,
                                                                   attributes: [NSFontAttributeName: UIFont.systemFontOfSize(11.0)],
                                                                   context: nil)

    let size = CGSizeMake(collectionView.bounds.width, ceil(boundingRect.height))

    return size
}

Just be sure to make up for the top/bottom/left/right margins of your label to the cell.

Adeel Miraj
  • 2,472
  • 3
  • 22
  • 32