# LBAttributeLayout **Repository Path**: liuboliu/lbattribute-layout ## Basic Information - **Project Name**: LBAttributeLayout - **Description**: 一个自适应宽度的属性样式自定义layout ,支持 计算高度,扩展性强,可灵活配置,一键导入 pod 'LBAttributeLayout' - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2021-09-21 - **Last Updated**: 2021-09-21 ## Categories & Tags **Categories**: Uncategorized **Tags**: sku布局, 自适应宽度, 标签layout ## README # LBAttributeLayout [![CI Status](https://img.shields.io/travis/liuboliu/LBAttributeLayout.svg?style=flat)](https://travis-ci.org/liuboliu/LBAttributeLayout) [![Version](https://img.shields.io/cocoapods/v/LBAttributeLayout.svg?style=flat)](https://cocoapods.org/pods/LBAttributeLayout) [![License](https://img.shields.io/cocoapods/l/LBAttributeLayout.svg?style=flat)](https://cocoapods.org/pods/LBAttributeLayout) [![Platform](https://img.shields.io/cocoapods/p/LBAttributeLayout.svg?style=flat)](https://cocoapods.org/pods/LBAttributeLayout) ## Example To run the example project, clone the repo, and run `pod install` from the Example directory first. ## Requirements ## Installation LBAttributeLayout is available through [CocoaPods](https://cocoapods.org). To install it, simply add the following line to your Podfile: ```ruby pod 'LBAttributeLayout' #使用方法 - (UICollectionView *)collectionView { if (!_collectionView) { LBAttributeLayout *layout = [[LBAttributeLayout alloc] initWithConfiguration:self.configuration]; layout.delegate = self; _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 100, CGRectGetWidth(self.view.bounds), 200) collectionViewLayout:layout]; [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([UICollectionViewCell class])]; _collectionView.dataSource = self; _collectionView.delegate = self; } return _collectionView; } - (LBAttributeLayoutConfiguration *)configuration { if (!_configuration) { _configuration = [[LBAttributeLayoutConfiguration alloc] init]; } return _configuration; } #实现 LBAttributeLayoutDelegate 协议方法 #pragma mark - LBAttributeLayoutDelegate - (CGFloat)cellWidthForItemAtIndex:(NSInteger)index { NSString *content = self.dataSource[index]; CGFloat width = [content sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18]}].width; return width + 30; } #计算内容高度 NSMutableArray *widthArray = [NSMutableArray array]; for (int i = 0; i < self.dataSource.count; i ++) { CGFloat width = [self cellWidthForItemAtIndex:i]; [widthArray addObject:@(width)]; } CGFloat height = [LBAttributeLayout contentHeightWithConfiguration:self.configuration cellWidthArray:widthArray]; self.collectionView.frame = CGRectMake(0, 100, CGRectGetWidth(self.view.bounds), height); ``` ## Demonstrate ![Image text](https://gitee.com/liuboliu/lbattribute-layout/raw/master/屏幕快照%202021-09-21%20下午12.58.44.png) ## Author liuboliu, 2534660131@qq.com ## License LBAttributeLayout is available under the MIT license. See the LICENSE file for more info.