YOUPAIZYLabelView.m 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //
  2. // YOUPAIZYLabelView.m
  3. // VQU
  4. //
  5. // Created by Elaine on 2020/12/16.
  6. // Copyright © 2020 leo. All rights reserved.
  7. //
  8. #import "YOUPAIZYLabelView.h"
  9. #import "UICollectionViewLeftAlignedLayout.h"
  10. #import "YOUPAILZLabelCollectionCell.h"
  11. @interface YOUPAIZYLabelView()<UICollectionViewDelegate , UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
  12. @property(nonatomic,strong)NSArray* youpaiplabelArray;
  13. @property(nonatomic,strong)NSString* youpaiptype;
  14. @end
  15. @implementation YOUPAIZYLabelView
  16. - (instancetype)initWithFrame:(CGRect)frame labelArray:(nonnull NSArray *)labelArray andType:(NSString *)type{
  17. if (self = [super initWithFrame:frame]) {
  18. self.backgroundColor = [UIColor clearColor];
  19. // NSMutableArray *arr = [NSMutableArray arrayWithArray:labelArray];
  20. // [arr addObjectsFromArray:labelArray];
  21. self.youpaiplabelArray = labelArray;
  22. self.youpaiptype = type;
  23. [self youpaifsetupView];
  24. }
  25. return self;
  26. }
  27. -(void)youpaifsetupView
  28. {
  29. if ([self.youpaiptype isEqualToString:@"userinfo"]) {
  30. UILabel* tipLabel = [[UILabel alloc]initWithFrame:CGRectMake(16,25,100,15)];
  31. tipLabel.textColor = LCTextNormal;
  32. tipLabel.font = LCFont12;
  33. tipLabel.text = @"标签";
  34. [self addSubview:tipLabel];
  35. }
  36. if ([self.youpaiptype isEqualToString:@"search"]) {
  37. // UIImageView *iconImgV = [[UIImageView alloc] initWithFrame:CGRectMake(12,26, 14, 11.5)];
  38. // iconImgV.image = [UIImage imageNamed:@"ic_tag"];
  39. // [self addSubview:iconImgV];
  40. //
  41. // UILabel* tipLabel = [[UILabel alloc]initWithFrame:CGRectMake(33,23,100,17)];
  42. // tipLabel.textColor = [UIColor whiteColor];
  43. // tipLabel.font = LCFont16;
  44. // tipLabel.text = @"标签";
  45. // [self addSubview:tipLabel];
  46. }
  47. [self youpaifbuildCollectionView];
  48. NSLog(@"%lf", self.youpaipcollectionView.collectionViewLayout.collectionViewContentSize.height);
  49. }
  50. - (void)youpaifbuildCollectionView{
  51. _youpaipcollectionView = ({
  52. UICollectionViewLeftAlignedLayout *layout = [[UICollectionViewLeftAlignedLayout alloc] init];
  53. CGRect rect = CGRectMake(0,40,KScreenWidth,self.frame.size.height-40);
  54. if ([self.youpaiptype isEqualToString:@"search"]) {
  55. rect = CGRectMake(0,0,KScreenWidth,self.frame.size.height);
  56. }
  57. UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:rect collectionViewLayout:layout];
  58. collectionView.backgroundColor = [UIColor clearColor];
  59. collectionView.delegate = self;
  60. collectionView.dataSource = self;
  61. collectionView.showsVerticalScrollIndicator = NO;
  62. //注册
  63. [collectionView registerClass:[YOUPAILZLabelCollectionCell class] forCellWithReuseIdentifier:@"YOUPAILZLabelCollectionCell"];
  64. collectionView;
  65. });
  66. [self addSubview:_youpaipcollectionView];
  67. // NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
  68. // [self collectionView:self.collectionView didSelectItemAtIndexPath:indexPath];
  69. }
  70. #pragma mark - <UICollectionViewDataSource>
  71. - (NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  72. return 1;
  73. }
  74. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  75. return _youpaiplabelArray.count;
  76. }
  77. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  78. YOUPAILZLabelCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"YOUPAILZLabelCollectionCell" forIndexPath:indexPath];
  79. YOUPAIZYLabelModel *model = _youpaiplabelArray[indexPath.row];
  80. model.youpaipisSelect = YES;
  81. [cell youpaifreloadWithModel:model];
  82. return cell;
  83. }
  84. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
  85. UICollectionReusableView *reusableview = nil;
  86. return reusableview;
  87. }
  88. //这里我为了直观的看出每组的CGSize设置用if 后续我会用简洁的三元表示
  89. #pragma mark - item宽高
  90. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  91. YOUPAIZYLabelModel *model = _youpaiplabelArray[indexPath.row];
  92. NSString *str = model.youpaipname;
  93. CGRect rect = [str boundingRectWithSize:CGSizeMake(KScreenWidth,30) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName :LCFont(11)} context:nil];
  94. return CGSizeMake(rect.size.width + 30 ,30);
  95. }
  96. #pragma mark - head宽高
  97. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
  98. return CGSizeZero;
  99. }
  100. #pragma mark - foot宽高
  101. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
  102. return CGSizeZero;
  103. }
  104. #pragma mark - <UICollectionViewDelegateFlowLayout>
  105. #pragma mark - X间距
  106. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
  107. return 10.0;
  108. }
  109. #pragma mark - Y间距
  110. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
  111. return 10.0;
  112. }
  113. //设置段落的内边距
  114. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
  115. {
  116. return UIEdgeInsetsMake(10,16,20,10);//UIEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right)
  117. }
  118. -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  119. {
  120. YOUPAIZYLabelModel *model = _youpaiplabelArray[indexPath.row];
  121. [self.youpaipdelegate youpaifclickLabel:model];
  122. }
  123. @end