set Dynamic width and height of collection view cell

Virus

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.

Saad Chaudhry

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)
                                     options:NSStringDrawingUsesLineFragmentOrigin
                                  attributes:attributes
                                     context:nil];
    //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];  

        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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Dynamic-height cell in collection view

From Dev

Dynamic-height cell in collection view

From Dev

how to set dynamic height of a Collection View, the 'view' not the 'cells'?

From Dev

How to set Dynamic width and height for an inflated view(button)

From Dev

How can I set the collection cell width to dynamic stretch to phone width

From Dev

How to give width and height programatically for collection view cell in ios, swift 3

From Dev

Change collection view cell width at runtime

From Dev

Fill width of button inside collection view cell

From Dev

collection view cell Hight and width in swift

From Dev

Dynamic reports -set width and height of report

From Dev

How to set a view height to be equal to its width

From Dev

How to set specific width/height for view programmatically?

From Dev

Android set image view height according to width

From Dev

How to set specific width/height for view programmatically?

From Dev

Swift Collection View custom layout change cell width in cell side

From Dev

Set height of UITableView which contain dynamic Cell Height

From Dev

Android: Is it possible to set View height proportional to the View width?

From Dev

Android: Is it possible to set View height proportional to the View width?

From Dev

Collection View cell height reverting back to default size

From Dev

how to set collection view cell as equal row cell for all screen

From Dev

Label in dynamic cell height on table view not showing text properly

From Dev

Scroll table view to bottom when using dynamic cell height

From Dev

ios swift - table view cell dynamic height depending on device

From Dev

Scroll table view to bottom when using dynamic cell height

From Dev

PHPExcel How to apply styles and set cell width and cell height to cell generated dynamically

From Dev

Dynamic canvas width and height

From Dev

Enlarge ImageView to cell size in Dynamic Collection view prototype cells

From Dev

JTable set column width with a dynamic size and JTextArea as cell renderer

From Dev

UICollectionView - dynamic cell height?

Related Related

  1. 1

    Dynamic-height cell in collection view

  2. 2

    Dynamic-height cell in collection view

  3. 3

    how to set dynamic height of a Collection View, the 'view' not the 'cells'?

  4. 4

    How to set Dynamic width and height for an inflated view(button)

  5. 5

    How can I set the collection cell width to dynamic stretch to phone width

  6. 6

    How to give width and height programatically for collection view cell in ios, swift 3

  7. 7

    Change collection view cell width at runtime

  8. 8

    Fill width of button inside collection view cell

  9. 9

    collection view cell Hight and width in swift

  10. 10

    Dynamic reports -set width and height of report

  11. 11

    How to set a view height to be equal to its width

  12. 12

    How to set specific width/height for view programmatically?

  13. 13

    Android set image view height according to width

  14. 14

    How to set specific width/height for view programmatically?

  15. 15

    Swift Collection View custom layout change cell width in cell side

  16. 16

    Set height of UITableView which contain dynamic Cell Height

  17. 17

    Android: Is it possible to set View height proportional to the View width?

  18. 18

    Android: Is it possible to set View height proportional to the View width?

  19. 19

    Collection View cell height reverting back to default size

  20. 20

    how to set collection view cell as equal row cell for all screen

  21. 21

    Label in dynamic cell height on table view not showing text properly

  22. 22

    Scroll table view to bottom when using dynamic cell height

  23. 23

    ios swift - table view cell dynamic height depending on device

  24. 24

    Scroll table view to bottom when using dynamic cell height

  25. 25

    PHPExcel How to apply styles and set cell width and cell height to cell generated dynamically

  26. 26

    Dynamic canvas width and height

  27. 27

    Enlarge ImageView to cell size in Dynamic Collection view prototype cells

  28. 28

    JTable set column width with a dynamic size and JTextArea as cell renderer

  29. 29

    UICollectionView - dynamic cell height?

HotTag

Archive