I have a view which contains UICollectionView that loads external cellnib file of UICollectionViewCell. I have the data showing properly just the way I want. I want to keep the cell height based on the textlabel inside it but I am not sure what am I missing in the code below. I have tried going through answers but it did not make much sense on how to tackle it. Please help. Thanks in advance.
- (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
MyCollectionViewCell *cell = (MyCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@" mycell" forIndexPath:indexPath];
NSString *string = cell.lblContact.text;
CGSize size = [self getLabelSize:string];
NSLog(@"%f and %f",size.width ,size.height);
return cell;
}
- (CGSize)getLabelSize:(NSString *)string {
UIFont *updatedFont = [UIFont fontWithName:@"myFont" size:18.0];
CGRect rect = [string boundingRectWithSize:(CGSize){maxWidth, CGFLOAT_MAX} options:NSStringDrawingUsesLineFragmentOrigin attributes:@{ NSFontAttributeName: updatedFont } context:nil];
return rect.size;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
CGSize retval = CGSizeMake(collectionView.frame.size.width, 75);
return retval;
}
- (UIEdgeInsets)collectionView: (UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
return UIEdgeInsetsMake(0, 0, 0, 0);
}
You retrieve the size of the label, but it looks like you are using a different CGSize to size the label. Instead of
CGSizeMake(collectionView.frame.size.width, 75), try keeping a reference to the size of the label and using that as the return value:EDIT
Have you tried accessing the frame of the label and storing it?