I have a UILabel in a UICollectionViewCell in a class named CollectionViewCell its all working. But the problem comes when the UILabel is smaller than the text I want to put in.
I've already made the [cell.label sizeToFit]; in the cellForItemAtIndexPath.
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *news = [self.select objectAtIndex:indexPath.row];
CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
cell.label.text = [news objectForKey:@"content"];
[cell.label setFont:[UIFont systemFontOfSize:15]];
cell.label.numberOfLines = 0;
[cell.label sizeToFit];
cell.label.preferredMaxLayoutWidth = 292.f;
return cell;
}
Already made the constraints like this answers https://stackoverflow.com/a/16009707/2577738 and it works only with the 7 first cells, the others just have a height of 29, here's the code that I implemented to make size of the cell variable.
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
CollectionViewCell *cell = (CollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
int h = 29 + cell.label.frame.size.height;
return CGSizeMake(292, h);
}
this is the result in some it worked