YOUPAILZHomeTagLabelView.m 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. //
  2. // YOUPAILZHomeTagLabelView.m
  3. // VQU
  4. //
  5. // Created by 肖浩然的mac on 2021/8/16.
  6. // Copyright © 2021 leo. All rights reserved.
  7. //
  8. #import "YOUPAILZHomeTagLabelView.h"
  9. #import "UICollectionViewLeftAlignedLayout.h"
  10. #import "YOUPAILZHomeTagLabelCell.h"
  11. #import "YOUPAILZlaber_listModel.h"
  12. @interface YOUPAILZHomeTagLabelView()<UICollectionViewDelegate , UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
  13. @property(nonatomic,strong)NSArray* youpaiplabelArray;
  14. @property(nonatomic,strong)NSString* youpaiptype;
  15. @property(nonatomic,strong)UICollectionView *youpaipcollectionView;
  16. @end
  17. @implementation YOUPAILZHomeTagLabelView
  18. - (instancetype)initWithFrame:(CGRect)frame LabelArr:(NSArray*)labelarr{
  19. if (self = [super initWithFrame:frame]){
  20. [self youpaifsetSubView];
  21. _youpaiplabelArray = labelarr;
  22. }
  23. return self;
  24. }
  25. -(void)setYoupaiplabArr:(NSArray *)labArr{
  26. _youpaiplabArr = labArr;
  27. self.youpaiplabelArray = labArr;
  28. [self.youpaipcollectionView reloadData];
  29. }
  30. -(void)youpaifsetSubView{
  31. UICollectionViewLeftAlignedLayout *layout = [[UICollectionViewLeftAlignedLayout alloc] init];
  32. UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
  33. [self addSubview:collectionView];
  34. self.youpaipcollectionView = collectionView;
  35. [collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
  36. make.edges.mas_equalTo(0);
  37. }];
  38. collectionView.backgroundColor = [UIColor clearColor];
  39. collectionView.delegate = self;
  40. collectionView.dataSource = self;
  41. collectionView.showsVerticalScrollIndicator = NO;
  42. //注册
  43. [collectionView registerClass:[YOUPAILZHomeTagLabelCell class] forCellWithReuseIdentifier:@"YOUPAILZHomeTagLabelCell"];
  44. }
  45. #pragma mark - <UICollectionViewDataSource>
  46. - (NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  47. return 1;
  48. }
  49. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  50. return _youpaiplabelArray.count;
  51. }
  52. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  53. YOUPAILZHomeTagLabelCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"YOUPAILZHomeTagLabelCell" forIndexPath:indexPath];
  54. YOUPAILZlaber_listModel *model = _youpaiplabelArray[indexPath.row];
  55. [cell youpaifreloadWithModel:model];
  56. return cell;
  57. }
  58. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
  59. UICollectionReusableView *reusableview = nil;
  60. return reusableview;
  61. }
  62. //这里我为了直观的看出每组的CGSize设置用if 后续我会用简洁的三元表示
  63. #pragma mark - item宽高
  64. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  65. YOUPAILZlaber_listModel *model = _youpaiplabelArray[indexPath.row];
  66. NSString *str = [NSString stringWithFormat:@"%@%ld",model.youpaipname,model.youpaipnum];
  67. CGRect rect = [str boundingRectWithSize:CGSizeMake(KScreenWidth,15) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName :LCFont(10)} context:nil];
  68. return CGSizeMake(rect.size.width+15 ,15);
  69. }
  70. #pragma mark - head宽高
  71. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
  72. return CGSizeZero;
  73. }
  74. #pragma mark - foot宽高
  75. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
  76. return CGSizeZero;
  77. }
  78. #pragma mark - <UICollectionViewDelegateFlowLayout>
  79. #pragma mark - X间距
  80. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
  81. return 3.0;
  82. }
  83. #pragma mark - Y间距
  84. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
  85. return 10.0;
  86. }
  87. //设置段落的内边距
  88. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
  89. {
  90. return UIEdgeInsetsMake(0,3,0,0);//UIEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right)
  91. }
  92. @end