## Calculating height of UICollectionViewCell with text only

systemlayoutsizefitting uicollectionviewcell

trying to calculate height of a cell with specified width and cannot make it right. Here is a snippet. There are two columns specified by the custom layout which knows the column width.

let cell = TextNoteCell2.loadFromNib() var frame = cell.frame frame.size.width = columnWidth // 187.5 frame.size.height = 0 // it does not work either without this line. cell.frame = frame cell.update(text: note.text) cell.contentView.layoutIfNeeded() let size = cell.contentView.systemLayoutSizeFitting(CGSize(width: columnWidth, height: 0)) // 251.5 x 52.5 print(cell) // 187.5 x 0 return size.height

Both `size`

and `cell.frame`

are incorrect.

Cell has a text label inside with 16px margins on each label edge.

Thank you in advance.

To calculate the size for a `UILabel`

to fully display the given text, i would add a helper as below,

extension UILabel { public static func estimatedSize(_ text: String, targetSize: CGSize = .zero) -> CGSize { let label = UILabel(frame: .zero) label.numberOfLines = 0 label.text = text return label.sizeThatFits(targetSize) } }

Now that you know how much size is required for your text, you can calculate the cell size by adding the margins you specified in the cell i.e 16.0 on each side so, the calculation should be as below,

let intrinsicMargin: CGFloat = 16.0 + 16.0 let targetWidth: CGFloat = 187.0 - intrinsicMargin let labelSize = UILabel.estimatedSize(note.text, targetSize: CGSize(width: targetWidth, height: 0)) let cellSize = CGSize(width: labelSize.width + intrinsicMargin, height: labelSize.height + intrinsicMargin)

Hope you will get the required results. One more improvement would be to calculate the width based on the screen size and number of columns instead of hard coded 187.0

**ios - Calculating height of UICollectionViewCell with text only,** To calculate the size for a UILabel to fully display the given text, i would add a helper as below, extension UILabel { public static func 3. Create a common cell configuration method, so that the cell set up and the cell height calculation methods use the same one. 4. Create an instance of the TestCell, to be used only for cell size calculations. Do not use this cell in the collection view dequeue. 5. This is the key part of the cell height calculation.

That cell you are loading from a nib has no view to be placed in, so it has an incorrect frame.

You need to either manually add it to a view, then measure it, or you'll need to dequeu it from the collectionView so it's already within a container view

**Calculating height of UICollectionViewCell with text only,** To calculate the size for a UILabel to fully display the given text, i would add a helper as below, extension UILabel { public static func estimatedSize(_ text: String, In my case, my UICollectionViewCell looks like this: As you see, there is the image on the left followed by 3 labels which are dynamic in height depending on how many text there will be. So lets start by creating a new UICollectionViewCell by selecting File > New > File > Cocoa Touch Class and

For Swift 4.2 updated answer is to handle height and width of uicollectionview Cell on the basis of uilabel text

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { let size = (self.FILTERTitles[indexPath.row] as NSString).size(withAttributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 14.0)]) return CGSize(width: size.width + 38.0, height: size.height + 25.0) }

**[iOS] UICollectionViewCell custom heights with autolayout and ,** However, sometimes a custom, manual, height calculation is required. Examples titleLabel.text = "Ooh woo, I'm a rebel just for kicks, now. I been feeling it Let’s say you have a constraint that says the width of your UILabel should be equal to 2/3 the width of your cell’s contentView. This means that you have to know the width of one in order to know the width of the other. Either you can: Define the label width and have the cell width follow (i.e. the self sizing way).

**UICollectionView With Autosizing Cells Based on Texts or Images,** Calculating Layout Attributes. For this layout, you need to dynamically calculate the height of every item since you don't know what the height of the photo or how Calculations for a rectangular prism: 1. Given the length, width and height find the volume, surface area and diagonal of a rectangular prism. h, l and w are known; find V, S and d; V = lwh; S = 2(lw + lh + wh) d = √(l 2 + w 2 + h 2) 2. Given the surface area, length and width find the height, volume and diagonal of a rectangular prism

**Accessible UICollectionViews With Dynamic Type and Self-Sizing ,** Should you only need certain cells to self-size then you may Implementing self-sizing UICollectionViewCells is a little more involved but Calculating the height of text in a UILabel can be achieved using the NSString Feet And Inches To Inches Calculator It is designed to convert an entry in feet and inches, or, in feet alone or in inches alone, into inches. The results are the total of the feet and inches entered, converted into inches.

**Dynamic Cell Sizing in UICollectionView,** For example, the intrinsicContentSize of a UILabel is the size of the label's text. Auto Layout will use the size of those views to calculate the size of your cell based We’re going to calculate the height by: Looping through our data to find the largest cell content. Rendering a sizing cell with said content. Calculating the height of the sizing cell. Adjusting the itemSize of the collectionView’s flowLayout, and height constraint on the collectionView itself, based on the height of the sizing cell.

##### Comments

- Hmmm, okay. I see this is simpler to calculate width and height of a view hierarchy inside a cell instead of calculating entire cell by instantiating it. I'll try this appraoch then. FYI I was able to calculate height of a label with your code. Thanks for your help.
- Yes, mostly labels mess the size of the cell because of text length so if we know that then we can easily calculate the total size of the cell by adding margins/paddings.
- When I dequeue cell and call
`layoutIfNeeded`

on it, the result is frame`(0; 0; 187.5; 1.79769e+308)`

which is still incorrect.