// // YOUPAIZYLabelView.m // VQU // // Created by Elaine on 2020/12/16. // Copyright © 2020 leo. All rights reserved. // #import "YOUPAIZYLabelView.h" #import "UICollectionViewLeftAlignedLayout.h" #import "YOUPAILZLabelCollectionCell.h" @interface YOUPAIZYLabelView() @property(nonatomic,strong)NSArray* youpaiplabelArray; @property(nonatomic,strong)NSString* youpaiptype; @end @implementation YOUPAIZYLabelView - (instancetype)initWithFrame:(CGRect)frame labelArray:(nonnull NSArray *)labelArray andType:(NSString *)type{ if (self = [super initWithFrame:frame]) { self.backgroundColor = [UIColor clearColor]; // NSMutableArray *arr = [NSMutableArray arrayWithArray:labelArray]; // [arr addObjectsFromArray:labelArray]; self.youpaiplabelArray = labelArray; self.youpaiptype = type; [self youpaifsetupView]; } return self; } -(void)youpaifsetupView { if ([self.youpaiptype isEqualToString:@"userinfo"]) { UILabel* tipLabel = [[UILabel alloc]initWithFrame:CGRectMake(16,25,100,15)]; tipLabel.textColor = LCTextNormal; tipLabel.font = LCFont12; tipLabel.text = @"标签"; [self addSubview:tipLabel]; } if ([self.youpaiptype isEqualToString:@"search"]) { // UIImageView *iconImgV = [[UIImageView alloc] initWithFrame:CGRectMake(12,26, 14, 11.5)]; // iconImgV.image = [UIImage imageNamed:@"ic_tag"]; // [self addSubview:iconImgV]; // // UILabel* tipLabel = [[UILabel alloc]initWithFrame:CGRectMake(33,23,100,17)]; // tipLabel.textColor = [UIColor whiteColor]; // tipLabel.font = LCFont16; // tipLabel.text = @"标签"; // [self addSubview:tipLabel]; } [self youpaifbuildCollectionView]; NSLog(@"%lf", self.youpaipcollectionView.collectionViewLayout.collectionViewContentSize.height); } - (void)youpaifbuildCollectionView{ _youpaipcollectionView = ({ UICollectionViewLeftAlignedLayout *layout = [[UICollectionViewLeftAlignedLayout alloc] init]; CGRect rect = CGRectMake(0,40,KScreenWidth,self.frame.size.height-40); if ([self.youpaiptype isEqualToString:@"search"]) { rect = CGRectMake(0,0,KScreenWidth,self.frame.size.height); } UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:rect collectionViewLayout:layout]; collectionView.backgroundColor = [UIColor clearColor]; collectionView.delegate = self; collectionView.dataSource = self; collectionView.showsVerticalScrollIndicator = NO; //注册 [collectionView registerClass:[YOUPAILZLabelCollectionCell class] forCellWithReuseIdentifier:@"YOUPAILZLabelCollectionCell"]; collectionView; }); [self addSubview:_youpaipcollectionView]; // NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; // [self collectionView:self.collectionView didSelectItemAtIndexPath:indexPath]; } #pragma mark - - (NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 1; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return _youpaiplabelArray.count; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { YOUPAILZLabelCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"YOUPAILZLabelCollectionCell" forIndexPath:indexPath]; YOUPAIZYLabelModel *model = _youpaiplabelArray[indexPath.row]; model.youpaipisSelect = YES; [cell youpaifreloadWithModel:model]; return cell; } - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { UICollectionReusableView *reusableview = nil; return reusableview; } //这里我为了直观的看出每组的CGSize设置用if 后续我会用简洁的三元表示 #pragma mark - item宽高 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { YOUPAIZYLabelModel *model = _youpaiplabelArray[indexPath.row]; NSString *str = model.youpaipname; CGRect rect = [str boundingRectWithSize:CGSizeMake(KScreenWidth,30) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName :LCFont(11)} context:nil]; return CGSizeMake(rect.size.width + 30 ,30); } #pragma mark - head宽高 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section { return CGSizeZero; } #pragma mark - foot宽高 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section { return CGSizeZero; } #pragma mark - #pragma mark - X间距 - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section { return 10.0; } #pragma mark - Y间距 - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section { return 10.0; } //设置段落的内边距 - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section { return UIEdgeInsetsMake(10,16,20,10);//UIEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right) } -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { YOUPAIZYLabelModel *model = _youpaiplabelArray[indexPath.row]; [self.youpaipdelegate youpaifclickLabel:model]; } @end