luke luke - 1 year ago 58
iOS Question

text for UILabel inside cell not fully displaying

I am attempting to display text that is fetched from somewhere to be display in my cell like so:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reviewCellID, forIndexPath: indexPath) as! ReviewCell

if let comment =[indexPath.row].comment {
cell.commentLabel.text = comment

return cell

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

if let comment =[indexPath.row].comment {
let height:CGFloat = estimateFrameForText(comment).height
return CGSizeMake(collectionView.frame.width - 12, height + 70)

func estimateFrameForText(text: String) -> CGRect {
let size = CGSize(width: collectionView.frame.width - 36, height: 1000)
let options = NSStringDrawingOptions.UsesFontLeading.union(.UsesLineFragmentOrigin)
return NSString(string: text).boundingRectWithSize(size, options: options, attributes: [NSFontAttributeName : UIFont(name: "Helvetica Neue", size: 12.0)!], context: nil)

The problem is that not all the text is shown. Sometimes the text inside the cell can be pretty long. Therefore, it ends up not display all the text in some of the cells once the user scrolls up and down the collectionview. See below where the text inside both cells are the exact same

enter image description here

I have never used a cache before but I think i need to use one here so the text is loaded inside the cell properly. I could be wrong.

Answer Source

Problem solved. All I needed to do was change UILabel to UITextView.