在開發標簽(Tag)展示類需求時,經常會遇到每個標簽寬度不一致的情況。IrregularGridCollectionView 就是一個專門用來處理不定寬度標簽 cell 的組件,能讓標簽自動換行並緊湊排列。
先看效果:


完整源碼可參考:https://github.com/YouXianMing/UI-Component-Collection 中的 IrregularGridCollectionView。
#import <UIKit/UIKit.h> #import "IrregularGridCellDataAdapter.h" #import "MaximumSpacingFlowLayout.h" #import "CustomIrregularGridViewCell.h" @protocol IrregularGridCollectionViewDelegate <NSObject> @optional - (void)irregularGridCollectionView:(IrregularGridCollectionView *)irregularGridCollectionView didSelectedCell:(CustomIrregularGridViewCell *)cell event:(id)event; @end @interface IrregularGridCollectionView : UIView @property (nonatomic, weak) id <IrregularGridCollectionViewDelegate> delegate; @property (nonatomic, strong, readonly) UICollectionView *collectionView; @property (nonatomic) UIEdgeInsets contentEdgeInsets; @property (nonatomic) CGFloat horizontalGap; @property (nonatomic) CGFloat verticalGap; @property (nonatomic) CGFloat gridHeight; @property (nonatomic, strong) NSArray <IrregularGridViewCellClassType *> *registerCells; @property (nonatomic, strong) NSMutableArray <IrregularGridCellDataAdapter *> *adapters; - (void)makeTheConfigEffective; @property (nonatomic, readonly) CGSize contentSize; - (void)resetSize; + (instancetype)irregularGridCollectionViewWithFrame:(CGRect)frame delegate:(id <IrregularGridCollectionViewDelegate>)delegate registerCells:(NSArray <IrregularGridViewCellClassType *> *)registerCells contentEdgeInsets:(UIEdgeInsets)edgeInsets verticalGap:(CGFloat)verticalGap horizontalGap:(CGFloat)horizontalGap gridHeight:(CGFloat)gridHeight; @end @interface IrregularGridViewCellClassType : NSObject @property (nonatomic) Class className; @property (nonatomic, strong) NSString *reuseIdentifier; @end NS_INLINE IrregularGridViewCellClassType *gridViewCellClassType(Class className, NSString *reuseIdentifier) { IrregularGridViewCellClassType *type = [IrregularGridViewCellClassType new]; type.className = className; type.reuseIdentifier = reuseIdentifier; return type; }
#import "IrregularGridCollectionView.h" @interface IrregularGridCollectionView () <UICollectionViewDelegate, UICollectionViewDataSource, CustomIrregularGridViewCellDelegate> @property (nonatomic, strong) UICollectionView *collectionView; @property (nonatomic, strong) UICollectionViewFlowLayout *flowLayout; @end @implementation IrregularGridCollectionView - (void)layoutSubviews { [super layoutSubviews]; _collectionView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height); } - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { self.contentEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5); self.horizontalGap = 5.f; self.verticalGap = 5.f; self.gridHeight = 20.f; self.flowLayout = [[MaximumSpacingFlowLayout alloc] init]; self.collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:self.flowLayout]; self.collectionView.showsHorizontalScrollIndicator = NO; self.collectionView.showsVerticalScrollIndicator = NO; self.collectionView.backgroundColor = [UIColor clearColor]; self.collectionView.delegate = self; self.collectionView.dataSource = self; [self addSubview:self.collectionView]; } return self; } - (void)makeTheConfigEffective { self.collectionView.contentInset = self.contentEdgeInsets; self.flowLayout.minimumLineSpacing = self.verticalGap; self.flowLayout.minimumInteritemSpacing = self.horizontalGap; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return _adapters.count; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { IrregularGridCellDataAdapter *adapter = _adapters[indexPath.row]; adapter.indexPath = indexPath; CustomIrregularGridViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:adapter.cellReuseIdentifier forIndexPath:indexPath]; cell.delegate = self; cell.dataAdapter = adapter; cell.data = adapter.data; cell.indexPath = indexPath; cell.collectionView = collectionView; cell.collectionGridView = self; [cell loadContent]; return cell; } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { IrregularGridCellDataAdapter *adapter = _adapters[indexPath.row]; return CGSizeMake(adapter.itemWidth, self.gridHeight); } @end

透過自定義的 MaximumSpacingFlowLayout,這個組件能在保證間距一致的前提下,讓不定寬度的 cell 緊湊地自動換行排列,非常適合用於標簽、關鍵詞等場景。