set Dynamic width and height of collection view cell

瘦欲@ 提交于 2019-12-01 13:21:31

问题


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

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


回答1:


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.




回答2:


In Swift 2.0:
Using a UICollectionViewFlowLayout enable cell auto-sizing.
The following code inside a UICollectionViewController subclass will enable auto-sizing for it’s flow layout.

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib. 
    if let cvl = collectionViewLayout as? UICollectionViewFlowLayout {
        cvl.estimatedItemSize = CGSize(width: 150, height: 75)
    }

}




回答3:


You can put different sizes in landscape & portrait mode try this -

- (CGSize)collectionView:(UICollectionView *)collectionView
                  layout:(UICollectionViewLayout *)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{    
    // Adjust cell size for orientation
    if (UIDeviceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])) {
        return CGSizeMake(rect.size.width, 50);
    }
    return CGSizeMake(rect.size.width, 50);
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    [self.collectionView performBatchUpdates:nil completion:nil];
}

you can call invalidateLayout method on the .collectionViewLayout property of your UICollectionView

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    [yourCollectionView.collectionViewLayout invalidateLayout];
}



回答4:


You can try this...

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(width, height);
}

width, height are the dynamically calculated value

Thanks



来源:https://stackoverflow.com/questions/38785870/set-dynamic-width-and-height-of-collection-view-cell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!