0

How can I a UICollectionViewCell with automate width size base size string?

1 Answer 1

1

You can do this to calculate the width from a string

let padding = 30

func collectionView(_ collectionView: UICollectionView,
                    layout collectionViewLayout: UICollectionViewLayout,
                    sizeForItemAt indexPath: IndexPath) -> CGSize {
    let lbl = UILabel() 
    lbl.text = arr[indexPath.item] // arr = [String]()
    let si = lbl.sizeThatFits(CGSize(width:CGFloat.greatestFiniteMagnitude, height:40)) 
    print(si.width)
    return CGSize(width:si.width + padding , height:40)
}

class VCName:UIViewController , UICollectionViewDelegate  , UICollectionViewDataSource , UICollectionViewDelegateFlowLayout { }
Sign up to request clarification or add additional context in comments.

2 Comments

@ Sh_Khan. How to use sizeForItemAt?
@ Sh_Khan. Thank's a lot. ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.