Virus Virus - 1 year ago 114
iOS Question

set Dynamic width and height of collection view cell

Actually i want to set button dynamically and title of button appear Dynamic i have try this and got width of Dynamic content but am not setting cell width and height.

- (void)viewDidLoad {
[super viewDidLoad];

ContentArray = [[NSMutableArray alloc]init];

self.collectionview.delegate = self;
self.collectionview.dataSource =self;
self.collectionview.backgroundColor = [UIColor clearColor];

[ContentArray addObject:@"Planning"];
[ContentArray addObject:@"Resources"];
[ContentArray addObject:@"Human Resources"];
[ContentArray addObject:@"Agriculture"];
[ContentArray addObject:@"Resources management"];


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
return ContentArray.count;

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

CollectionViewCell * Cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionViewCell"forIndexPath:indexPath];

[Cell.button setTitle:[ContentArray objectAtIndex:indexPath.row] forState:UIControlStateNormal];

return Cell;

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath

CGRect rect = [[ContentArray objectAtIndex:indexPath.row ] boundingRectWithSize:CGSizeMake(HUGE_VALF, 50) options:NSStringDrawingUsesFontLeading attributes:nil context:nil] ;

return CGSizeMake(rect.size.width, 50);

and output is like this

OUTPUT look like this when i used this code

This is perfect i think if i make cell dynamic width and height, so please guide me through this.

Answer Source

pragma mark - GetHeightFromString

- (CGFloat)getTextHeightFromString:(NSString *)text ViewWidth:(CGFloat)width WithPading:(CGFloat)pading FontName:(NSString *)fontName AndFontSize:(CGFloat)fontSize
    NSDictionary *attributes = @{NSFontAttributeName: [UIFont fontWithName:fontName size:fontSize]};
    CGRect rect = [text boundingRectWithSize:CGSizeMake(width, MAXFLOAT)
    //NSLog(@"rect.size.height: %f", rect.size.height);
    return rect.size.height + pading;

Get height of the string from this method and add this to origional height of your cell or label or button whatever you want to use.

For example:

  - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
        YourCellClass *myCell =  (YourCellClass*) [collectionView cellForItemAtIndexPath:indexPath.row];  

        CGFloat  sizeFromString   = [self getTextHeightFromString:itemObject.MenuDescription ViewWidth:myCell.frame.size.width WithPading:10 FontName:@"HelveticaNeue" AndFontSize:13];

        return CGSizeMake(sizeFromString, sizeFromString);

You might need to change the height of inner label too, for this use same method in cellForItemAtIndexPath method of your UICollectionView. Further more you can customize your own way.