ios - 设置 Collection View 单元格的动态宽度和高度

标签 ios objective-c uicollectionview uicollectionviewcell uicollectionviewlayout

实际上我想动态设置按钮并且按钮的标题显示为动态我已经尝试过并获得动态内容的宽度但我没有设置单元格宽度和高度。

- (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);
    }

输出是这样的

OUTPUT look like this when i used this code

如果我让单元格动态宽度和高度,我认为这是完美的,所以请指导我完成这个。

最佳答案

编译指示标记 - 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;
}

从此方法获取字符串的高度,并将其添加到您想要使用的单元格或标签或按钮的原始高度。

例如:

  - (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);
    }

您可能还需要更改内部标签的高度,为此请在 UICollectionViewcellForItemAtIndexPath 方法中使用相同的方法。 此外,您还可以自定义自己的方式。

关于ios - 设置 Collection View 单元格的动态宽度和高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38785870/

相关文章:

ios - 在 if 语句中初始化不同单元格类型的更好方法

iphone - 截断 NSString 以适应大小?

iOS 格式指定类型 int 但参数类型为 int *

ios - NSAssert 会占用 iOS 设备上的任何内存吗?

ios - 扩展 UICollectionView 标题内容以到达第一个单元格下方

ios - UICollectionViewDelegateFlowLayout UICollectionViewFlowLayout 之间的区别

swift - 使用 Collection View 中的数据填充 TableView

ios - 如何四舍五入 NSNumber 并在对象 C 中创建一系列数字

ios - 获取当前前台应用的名称

ios - 如何像苹果一样从堆栈跟踪中了解崩溃发生在特定线程中?