UICollectionViewFlowLayout根据数据宽度左对齐实现

由于功能原因想实现根据长度左对齐并且超过边距换行输出的效果(效果如图)


xiaoguo.png

以下是实现效果的代码

#import <UIKit/UIKit.h>

@protocol SearchOfFlowLayoutDelegate <NSObject>

// 获取item高度
- (CGFloat)widthForItemIndexPath:(NSIndexPath *)indexPath AndCollectioinView:(UICollectionView *)collectionView;

@end

@interface SearchOfFlowLayout : UICollectionViewFlowLayout

@property (nonatomic, assign) id<SearchOfFlowLayoutDelegate>delegate;

// item 间距
@property (nonatomic,assign)CGFloat insertItemSpacing;

@end
#import "SearchOfFlowLayout.h"

#define WIDTH [UIScreen mainScreen].bounds.size.width

@interface SearchOfFlowLayout ()
//临时保存item的总宽度
@property (nonatomic, assign) CGFloat columnWidth;
//记录一共有多少行
@property (nonatomic, assign) NSInteger columnNumber;
//保存每一个item x y w h
@property (nonatomic, retain) NSMutableArray *arrForItemAtrributes;
//保存item总数
@property (nonatomic,assign) NSUInteger numberOfItems;
// 保存每个item的X值
@property (nonatomic, assign) CGFloat xForItemOrigin;
// 保存每个item的Y值
@property (nonatomic, assign) CGFloat yForItemOrigin;

@end


@implementation SearchOfFlowLayout

// 准备布局
- (void)prepareLayout {
    
    [super prepareLayout];
    
    self.columnWidth = self.insertItemSpacing;
    self.columnNumber = 0;
    self.arrForItemAtrributes = [NSMutableArray array];
    self.xForItemOrigin = self.insertItemSpacing;
    self.yForItemOrigin = self.insertItemSpacing;
    
    //获取item的个数
    self.numberOfItems = [self.collectionView numberOfItemsInSection:0];
    /** 为每个item确定LayoutAttribute属性,同时将这些属性放入布局数组中 */
    for(int i = 0;i < self.numberOfItems;i++)
    {
        /** 确定每个Item的indexPath属性 */
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
        /** 确定每个item的origin的x,y值 */
        /** 确定每个Item的frame属性,同时确定了每个Item的LayoutAttribute,放入到了布局属性数组中 */
        [self setFrame:indexPath];
    }

}
// 计算contentView的大小
- (CGSize)collectionViewContentSize
{
    // 获取collectionView的Size
    CGSize contentSize = self.collectionView.frame.size;
    // 最大高度+bottom
    contentSize.height = self.insertItemSpacing + (self.itemSize.height + self.insertItemSpacing) * (self.columnNumber + 1);
    //设置collectionView的大小自适应
    self.collectionView.frame = CGRectMake(self.collectionView.frame.origin.x, self.collectionView.frame.origin.y, contentSize.width, contentSize.height);
   return contentSize;
}

// 返回每一个item的attribute
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
    // 返回每一个item的Attribute
    return self.arrForItemAtrributes;
}

// 设置属性和frame
- (void)setFrame:(NSIndexPath *)indexPath
{
    // 设置Item LayoutAttribute 属性
    UICollectionViewLayoutAttributes *layoutArr = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    // 获取item的高
    CGFloat itemWidth = 0;
    if (_delegate && [_delegate respondsToSelector:@selector(widthForItemIndexPath:AndCollectioinView:)]) {
        // 使用代理方法获取item的高
        itemWidth = [_delegate widthForItemIndexPath:indexPath AndCollectioinView:self.collectionView];
    }
    //之前item的宽总和 + 当前item的宽 + 间距 < 屏幕总款
    if (self.columnWidth + itemWidth + self.insertItemSpacing < WIDTH) {
        //设置x
        self.xForItemOrigin = self.columnWidth;
        self.columnWidth += itemWidth + self.insertItemSpacing;
    }else {
        self.xForItemOrigin = self.insertItemSpacing;
        //如果宽度超过屏幕从新计算宽度
        self.columnWidth = itemWidth + self.insertItemSpacing * 2;
        self.columnNumber++;
    }
    // 计算是第几行 乘以高度
    self.yForItemOrigin = self.insertItemSpacing + (self.itemSize.height + self.insertItemSpacing) * self.columnNumber;
    
    // 设置frame
    layoutArr.frame = CGRectMake(self.xForItemOrigin, self.yForItemOrigin, itemWidth, self.itemSize.height);
    // 放入数组
    [self.arrForItemAtrributes addObject:layoutArr];
}
@end

以下是实现代理的方法 根据文本的长度判断文本的宽度, 根据tag控制是哪个collection

- (CGFloat)widthForItemIndexPath:(NSIndexPath *)indexPath AndCollectioinView:(UICollectionView *)collectionView{
    if (collectionView.tag == 1001) {
        NSString *content = self.hotTagArr[indexPath.item];
        CGRect itemFrame = [content boundingRectWithSize:CGSizeMake(0, 20) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:17]} context:nil];
        
        CGFloat width = itemFrame.size.width + 10;
        return width;
    }
    return 10;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,334评论 4 61
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,800评论 19 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,901评论 25 709
  • 原以为我很坚强 能够应对生活的无常 ...
    葡萄树下的绿刺猬阅读 2,600评论 0 0
  • 拿起手机,爱奇艺推送过来一纪录片《生门》,真实记录四个女人,四个家庭生养孩子的过程,纠结着,还是打开看了。 百度上...
    宥奇迹阅读 4,640评论 0 1