酷店

MSSAutoresizeLabelFlow.m 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. //
  2. // MSSAutoresizeLabelFlow.m
  3. // MSSAutoresizeLabelFlow
  4. //
  5. // Created by Mrss on 15/12/26.
  6. // Copyright © 2015年 expai. All rights reserved.
  7. //
  8. #import "MSSAutoresizeLabelFlow.h"
  9. #import "MSSAutoresizeLabelFlowLayout.h"
  10. #import "MSSAutoresizeLabelFlowCell.h"
  11. #import "MSSAutoresizeLabelFlowConfig.h"
  12. static NSString *const cellId = @"cellId";
  13. @interface MSSAutoresizeLabelFlow () <UICollectionViewDataSource,UICollectionViewDelegate,MSSAutoresizeLabelFlowLayoutDataSource,MSSAutoresizeLabelFlowLayoutDelegate>
  14. @property (nonatomic,strong) UICollectionView *collection;
  15. @property (nonatomic,strong) NSMutableArray *data;
  16. @property (nonatomic, copy) selectedHandler handler;
  17. @end
  18. @implementation MSSAutoresizeLabelFlow
  19. - (instancetype)initWithFrame:(CGRect)frame titles:(NSArray *)titles selectedHandler:(selectedHandler)handler {
  20. self = [super initWithFrame:frame];
  21. if (!titles.count) {
  22. return self;
  23. }
  24. if (self) {
  25. self.backgroundColor = [UIColor whiteColor];
  26. self.data = [titles mutableCopy];
  27. self.handler = handler;
  28. [self setup];
  29. }
  30. return self;
  31. }
  32. - (void)setup {
  33. // UICollectionViewFlowLayout *layout =[[UICollectionViewFlowLayout alloc]init];
  34. MSSAutoresizeLabelFlowLayout *layout = [[MSSAutoresizeLabelFlowLayout alloc]init];
  35. layout.delegate = self;
  36. layout.dataSource = self;
  37. self.collection = [[UICollectionView alloc]initWithFrame:self.bounds collectionViewLayout:layout];
  38. self.collection.backgroundColor = [UIColor clearColor];
  39. self.collection.allowsMultipleSelection = YES;
  40. self.collection.delegate = self;
  41. self.collection.dataSource = self;
  42. [self.collection registerClass:[MSSAutoresizeLabelFlowCell class] forCellWithReuseIdentifier:cellId];
  43. [self addSubview:self.collection];
  44. }
  45. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  46. return 1;
  47. }
  48. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  49. return self.data.count;
  50. }
  51. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  52. MSSAutoresizeLabelFlowCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
  53. [cell configCellWithTitle:self.data[indexPath.item]];
  54. return cell;
  55. }
  56. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  57. if (self.handler) {
  58. NSUInteger index = indexPath.item;
  59. NSString *title = self.data[index];
  60. self.handler(index,title);
  61. }
  62. }
  63. - (NSString *)titleForLabelAtIndexPath:(NSIndexPath *)indexPath {
  64. return self.data[indexPath.item];
  65. }
  66. - (void)layoutFinishWithNumberOfline:(NSInteger)number {
  67. NSInteger numberCount = 0 ;
  68. if (numberCount == number) {
  69. return;
  70. }
  71. numberCount = number;
  72. MSSAutoresizeLabelFlowConfig *config = [MSSAutoresizeLabelFlowConfig shareConfig];
  73. CGFloat h = config.contentInsets.top+config.contentInsets.bottom+config.itemHeight*number+config.lineSpace*(number-1);
  74. self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, h);
  75. self.collection.frame = self.bounds;
  76. // [UIView animateWithDuration:0.1 animations:^{
  77. //
  78. // }];
  79. }
  80. - (void)insertLabelWithTitle:(NSString *)title atIndex:(NSUInteger)index animated:(BOOL)animated {
  81. NSIndexPath *indexPath = [NSIndexPath indexPathForItem:index inSection:0];
  82. [self.data insertObject:title atIndex:index];
  83. [self performBatchUpdatesWithAction:UICollectionUpdateActionInsert indexPaths:@[indexPath] animated:animated];
  84. }
  85. - (void)insertLabelsWithTitles:(NSArray *)titles atIndexes:(NSIndexSet *)indexes animated:(BOOL)animated {
  86. NSArray *indexPaths = [self indexPathsWithIndexes:indexes];
  87. [self.data insertObjects:titles atIndexes:indexes];
  88. [self performBatchUpdatesWithAction:UICollectionUpdateActionInsert indexPaths:indexPaths animated:animated];
  89. }
  90. - (void)deleteLabelAtIndex:(NSUInteger)index animated:(BOOL)animated {
  91. [self deleteLabelsAtIndexes:[NSIndexSet indexSetWithIndex:index] animated:animated];
  92. }
  93. - (void)deleteLabelsAtIndexes:(NSIndexSet *)indexes animated:(BOOL)animated {
  94. NSArray *indexPaths = [self indexPathsWithIndexes:indexes];
  95. [self.data removeObjectsAtIndexes:indexes];
  96. [self performBatchUpdatesWithAction:UICollectionUpdateActionDelete indexPaths:indexPaths animated:animated];
  97. }
  98. - (void)reloadAllWithTitles:(NSArray *)titles {
  99. self.data = [titles mutableCopy];
  100. [self.collection reloadData];
  101. }
  102. - (void)performBatchUpdatesWithAction:(UICollectionUpdateAction)action indexPaths:(NSArray *)indexPaths animated:(BOOL)animated {
  103. if (!animated) {
  104. [UIView setAnimationsEnabled:NO];
  105. }
  106. [self.collection performBatchUpdates:^{
  107. switch (action) {
  108. case UICollectionUpdateActionInsert:
  109. [self.collection insertItemsAtIndexPaths:indexPaths];
  110. break;
  111. case UICollectionUpdateActionDelete:
  112. [self.collection deleteItemsAtIndexPaths:indexPaths];
  113. default:
  114. break;
  115. }
  116. } completion:^(BOOL finished) {
  117. if (!animated) {
  118. [UIView setAnimationsEnabled:YES];
  119. }
  120. }];
  121. }
  122. - (NSArray *)indexPathsWithIndexes:(NSIndexSet *)set {
  123. NSMutableArray *indexPaths = [NSMutableArray arrayWithCapacity:set.count];
  124. [set enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
  125. NSIndexPath *indexPath = [NSIndexPath indexPathForItem:idx inSection:0];
  126. [indexPaths addObject:indexPath];
  127. }];
  128. return [indexPaths copy];
  129. }
  130. @end