酷店

MSSAutoresizeLabelFlowLayout.m 3.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. //
  2. // MSSAutoresizeLabelFlowLayout.m
  3. // MSSAutoresizeLabelFlow
  4. //
  5. // Created by Mrss on 15/12/26.
  6. // Copyright © 2015年 expai. All rights reserved.
  7. //
  8. #import "MSSAutoresizeLabelFlowLayout.h"
  9. #import "MSSAutoresizeLabelFlowConfig.h"
  10. typedef struct currentOrigin {
  11. CGFloat lineX;
  12. NSInteger lineNumber;
  13. }currentOrigin;
  14. @implementation MSSAutoresizeLabelFlowLayout {
  15. UIEdgeInsets contentInsets;
  16. CGFloat itemHeight;
  17. CGFloat itemSpace;
  18. CGFloat lineSpace;
  19. CGFloat itemMargin;
  20. UIFont *titleFont;
  21. NSInteger itemCount;
  22. currentOrigin orgin;
  23. }
  24. - (void)prepareLayout {
  25. [super prepareLayout];
  26. MSSAutoresizeLabelFlowConfig *config = [MSSAutoresizeLabelFlowConfig shareConfig];
  27. contentInsets = config.contentInsets;
  28. titleFont = config.textFont;
  29. lineSpace = config.lineSpace;
  30. itemHeight = config.itemHeight;
  31. itemSpace = config.itemSpace;
  32. itemCount = [self.collectionView numberOfItemsInSection:0];
  33. itemMargin = config.textMargin;
  34. orgin.lineNumber = 0;
  35. orgin.lineX = contentInsets.left;
  36. }
  37. - (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
  38. NSArray *attributesArray = [super layoutAttributesForElementsInRect:rect];
  39. for (NSInteger i = 0; i<attributesArray.count; i++) {
  40. UICollectionViewLayoutAttributes *att = attributesArray[i];
  41. NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
  42. NSString *title = [self.dataSource titleForLabelAtIndexPath:indexPath];
  43. CGSize size = [self sizeWithTitle:title font:titleFont];
  44. CGFloat itemOrginX = orgin.lineX;
  45. CGFloat itemOrginY = orgin.lineNumber*(itemHeight+lineSpace) + contentInsets.top;
  46. CGFloat itemWidth = size.width+itemMargin;
  47. if (itemWidth > CGRectGetWidth(self.collectionView.frame)-(contentInsets.left+contentInsets.right)) {
  48. itemWidth = CGRectGetWidth(self.collectionView.frame)-(contentInsets.left+contentInsets.right);
  49. }
  50. att.frame = CGRectMake(itemOrginX, itemOrginY, itemWidth, itemHeight);
  51. orgin.lineX += itemWidth+itemSpace;
  52. if (i < attributesArray.count-1) {
  53. NSIndexPath *nextIndexPath = [NSIndexPath indexPathForItem:i+1 inSection:0];
  54. NSString *nextTitle = [self.dataSource titleForLabelAtIndexPath:nextIndexPath];
  55. CGSize nextSize = [self sizeWithTitle:nextTitle font:titleFont];
  56. if (nextSize.width+itemMargin > CGRectGetWidth(self.collectionView.frame)-contentInsets.right-orgin.lineX) {
  57. orgin.lineNumber ++;
  58. orgin.lineX = contentInsets.left;
  59. }
  60. }
  61. else {
  62. [self.delegate layoutFinishWithNumberOfline:orgin.lineNumber+1];
  63. }
  64. }
  65. return attributesArray;
  66. }
  67. - (CGSize)sizeWithTitle:(NSString *)title font:(UIFont *)font {
  68. CGRect rect = [title boundingRectWithSize:CGSizeMake(1000, itemHeight) options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil];
  69. return rect.size;
  70. }
  71. @end