Resize UICollectionView cells after their data has been set

前端 未结 4 1353
日久生厌
日久生厌 2020-12-07 15:37

My UICollectionView cells contain UILabels with multiline text. I don\'t know the height of the cells until the text has been set on the label.

-(CGSize)c         


        
4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-07 16:34

    Hey in the above delegate method itself, you can calculate the UILabel size using the below tricky way and return the UICollectionViewCell size based on that calculation.

        // Calculate the expected size based on the font and 
        // linebreak mode of your label
        CGSize maximumLabelSize = CGSizeMake(9999,9999);
    
        CGSize expectedLabelSize = 
         [[self.dataSource objectAtIndex:indexPath.item] 
                 sizeWithFont:[UIFont fontWithName:@"Arial" size:18.0f] 
            constrainedToSize:maximumLabelSize 
                lineBreakMode:NSLineBreakByWordWrapping];
    

提交回复
热议问题