// // MSSAutoresizeLabelFlowLayout.m // MSSAutoresizeLabelFlow // // Created by Mrss on 15/12/26. // Copyright © 2015年 expai. All rights reserved. // #import "MSSAutoresizeLabelFlowLayout.h" #import "MSSAutoresizeLabelFlowConfig.h" typedef struct currentOrigin { CGFloat lineX; NSInteger lineNumber; }currentOrigin; @implementation MSSAutoresizeLabelFlowLayout { UIEdgeInsets contentInsets; CGFloat itemHeight; CGFloat itemSpace; CGFloat lineSpace; CGFloat itemMargin; UIFont *titleFont; NSInteger itemCount; currentOrigin orgin; } - (void)prepareLayout { [super prepareLayout]; MSSAutoresizeLabelFlowConfig *config = [MSSAutoresizeLabelFlowConfig shareConfig]; contentInsets = config.contentInsets; titleFont = config.textFont; lineSpace = config.lineSpace; itemHeight = config.itemHeight; itemSpace = config.itemSpace; itemCount = [self.collectionView numberOfItemsInSection:0]; itemMargin = config.textMargin; orgin.lineNumber = 0; orgin.lineX = contentInsets.left; } - (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect { NSArray *attributesArray = [super layoutAttributesForElementsInRect:rect]; for (NSInteger i = 0; i CGRectGetWidth(self.collectionView.frame)-(contentInsets.left+contentInsets.right)) { itemWidth = CGRectGetWidth(self.collectionView.frame)-(contentInsets.left+contentInsets.right); } att.frame = CGRectMake(itemOrginX, itemOrginY, itemWidth, itemHeight); orgin.lineX += itemWidth+itemSpace; if (i < attributesArray.count-1) { NSIndexPath *nextIndexPath = [NSIndexPath indexPathForItem:i+1 inSection:0]; NSString *nextTitle = [self.dataSource titleForLabelAtIndexPath:nextIndexPath]; CGSize nextSize = [self sizeWithTitle:nextTitle font:titleFont]; if (nextSize.width+itemMargin > CGRectGetWidth(self.collectionView.frame)-contentInsets.right-orgin.lineX) { orgin.lineNumber ++; orgin.lineX = contentInsets.left; } } else { [self.delegate layoutFinishWithNumberOfline:orgin.lineNumber+1]; } } return attributesArray; } - (CGSize)sizeWithTitle:(NSString *)title font:(UIFont *)font { CGRect rect = [title boundingRectWithSize:CGSizeMake(1000, itemHeight) options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil]; return rect.size; } @end