Collectionview height based on label text iOS swift

 Problem:

I want to show the collection view cell height to be dynamic based on the label text.How can i achieve that?


Solution:

    func collectionView(_ collectionView: UICollectionViewlayout collectionViewLayout: UICollectionViewLayoutsizeForItemAt indexPath: IndexPath) -> CGSize {

        let screenSize = UIScreen.main.bounds

        let size = (screenSize.width) / CGFloat(1)

        var collheight = 30.0

            if let item = mystring {

                let labelHeight = item.content?.height(withConstrainedWidth: collectionView.bounds.size.widthfontUIFont().medium()) // Adjust the font and other attributes as needed

                collheight = labelHeight! + 50

            }

        return CGSize(width: size, height: collheight)


}

Comments

Popular posts from this blog

Invalid bundle error while upload the app to the app Store

Convert NsNumber, NSDate to String in iOS Swift

Global LocationManager Singleton class iOS Swift