草庐IT

iOS UICollectionView的动态布局及内容自适应大小

人生若只如初见丶_a4e8 2023-09-17 原文

在日常开发中,经常涉及到一些条件按钮和内容标签的展示。有很多属性需要添加,都用按钮来实现显然太繁琐,也不太河里
image.png

,而且如果这些标签需要动态设置将变得更加复杂。
本文通过UICollectionView来实现这些需求。由于展示内容通过服务端动态控制,所以这里首先要做的就是让collectionView的cell大小能够自适应文字的宽度。然后才是动态设置collectionView的尺寸。

计算cell的大小

由于计算文字宽度的代码都是通用的,直接在sizeForItemAtIndexPath方法中返回cell的宽度和高度。

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *attribute = @{NSFontAttributeName: self.textFont};
    CGSize currentLabelSize = [self.dataAry[indexPath.item] sizeWithAttributes:attribute];
    CGFloat itemWidth = ceil(currentLabelSize.width) + itemWidthSpacing * 2;
// item内容超过collectionView宽度的时候,做布局处理
    if (itemWidth + self.flowLayout.minimumInteritemSpacing > self.flowLayout.contentWidth) {
        itemWidth = self.flowLayout.contentWidth - self.flowLayout.minimumInteritemSpacing;
    }
    return CGSizeMake(itemWidth, self.itemCellHeight);
}

但是设置完以后可能会发现item的间距并不是固定的,如下图:
image.png

重写UICollectionViewFlowLayout方法

要让collectionView整齐排布,就要用到flowLayout,这里通过继承UICollectionViewFlowLayout来重新对collectionView进行布局。UICollectionViewFlowLayout是一个布局类继承自UICollectionViewLayout,主要涉及以下两个方法:

- (CGSize)collectionViewContentSize; 

- (nullable NSArray<__kindof UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect;

collectionViewContentSize这个方法,用来返回collectionView的内容的大小,并不是UICollectionView的frame大小。
layoutAttributesForElementsInRect,这里就是返回所有布局属性的数组。
通过重写这两个方法,就可以重新对content进行布局了,这样每个cell的排列就可以紧凑起来了。

首先自定义个类继承自UICollectionViewFlowLayout,定义所需要的属性

@interface LiveBroadcastFlowLayout : UICollectionViewFlowLayout

///数组内容
@property (nonatomic, strong) NSArray *dataArry;
///item高度
@property (nonatomic, assign) CGFloat itemHeight;
///item左右偏移
@property (nonatomic, assign) CGFloat itemWidthSpacing;
///展示字体
@property (nonatomic, strong) UIFont *textFont;
///UICollectionView宽度
@property (nonatomic, assign) CGFloat contentWidth;

@end

通过代码注释,可以了解重写的方法及作用。
计算ContentSize的大小:

- (CGSize)collectionViewContentSize {
    CGSize size = CGSizeZero;
    NSInteger itemCount = 0;
    //获取collectionView的item个数,为0的话返回CGSizeZero
    if ([self.collectionView.dataSource respondsToSelector:@selector(collectionView:numberOfItemsInSection:)]) {
        itemCount = [self.collectionView.dataSource collectionView:self.collectionView numberOfItemsInSection:0];
    }
    if (CGSizeEqualToSize(size, CGSizeZero) && itemCount == 0) {
        return CGSizeZero;
    }
    // 内容宽度
    NSInteger lineWidth = 0;
    // 展示行数
    NSUInteger rowCount = 1;
    for (int i = 0; i < itemCount; ++i) {
        // self.dataArry为内容数组
        // 根据传入的字体大小self.textFont计算item宽度
        // 然后与传入的collectionView的宽度self.contentWidth做计算
        NSDictionary *attribute = @{NSFontAttributeName: self.textFont};
        CGSize currentLabelSize = [self.dataArry[i] sizeWithAttributes:attribute];
        //self.itemWidthSpacing为展示预留的宽度,根据需求设置
        CGFloat cellWidth = currentLabelSize.width + self.itemWidthSpacing * 2;
        lineWidth = lineWidth + self.minimumInteritemSpacing + cellWidth;

        // 最后一个item不考虑minimumInteritemSpacing
        if (i == itemCount - 1) {
            lineWidth = lineWidth - self.minimumInteritemSpacing;
        }

        // 计算一行的item展示数量
        if (lineWidth >= self.contentWidth) {
            // 最后一个文本累加长度等于self.contentWidth,不做换行
            if (i == (itemCount - 1) && lineWidth == self.contentWidth) {
                break;
            }
            // 最后一个文本大于self.contentWidth且独占一行,不做换行
            if (i == (itemCount - 1) && cellWidth >= self.contentWidth && (lineWidth - self.minimumInteritemSpacing) == cellWidth) {
                break;
            }
            lineWidth = 0;
            rowCount++;
        }
    }
    // 最终计算出collectionView内容展示所需的高度
    size.width = self.contentWidth;
    size.height = rowCount * self.itemHeight + (rowCount - 1) * self.minimumLineSpacing  + self.sectionInset.top + self.sectionInset.bottom;
    
    return size;
}

控制collectionView内部item布局的展示:

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
    NSMutableArray* attributes = [[super layoutAttributesForElementsInRect:rect] mutableCopy];
    //将第一个item固定在左上,防止一行只展示一个item时位置错乱
    if (attributes.count > 0) {
        UICollectionViewLayoutAttributes *currentLayoutAttributes = attributes[0];
        CGRect frame = currentLayoutAttributes.frame;
        frame.origin.x = 0;
        frame.origin.y = 0;
        currentLayoutAttributes.frame = frame;
    }
    
    for (int i = 1; i < [attributes count]; ++i) {
        NSDictionary *attribute = @{NSFontAttributeName: self.textFont};
        CGSize labelSize = [self.dataArry[i] sizeWithAttributes:attribute];
        UICollectionViewLayoutAttributes *currentLayoutAttributes = attributes[i];
        UICollectionViewLayoutAttributes *prevLayoutAttributes = attributes[i - 1];
        CGFloat cellWidth = ceil(labelSize.width) + self.itemWidthSpacing * 2;
        if (cellWidth + self.minimumInteritemSpacing > self.contentWidth) {
            cellWidth = self.contentWidth - self.minimumInteritemSpacing;
        }
        currentLayoutAttributes.size = CGSizeMake(cellWidth, self.itemHeight);
        NSInteger origin = CGRectGetMaxX(prevLayoutAttributes.frame);
        // 如果当前item的宽度+前一个item的最大宽度+item间距<=collectionView的宽度,则一行可以容纳下,修改当前item的x轴位置,否则居左显示
        if (origin + self.minimumInteritemSpacing + currentLayoutAttributes.frame.size.width < self.contentWidth) {
            CGRect frame = currentLayoutAttributes.frame;
            frame.origin.x = origin + self.minimumInteritemSpacing;
            currentLayoutAttributes.frame = frame;
        } else {
            CGRect frame = currentLayoutAttributes.frame;
            frame.origin.x = 0;
            // 原文说会自动调整,但是在item内容过长的时候会有问题,做如下处理
            frame.origin.y = CGRectGetMaxY(prevLayoutAttributes.frame) + self.minimumLineSpacing;
            currentLayoutAttributes.frame = frame;
        }
    }

    return attributes;
}

layoutAttributesForElementsInRect方法中,这里从第二个item开始,每个cell的位置都是前一个cell的位置+maximumSpacing,如果不超过这一行的最大宽度,就改变当前cell的起始位置和大小(也即frame)。如果超过了就不改变,那不改变是什么意思?就是保持原来的位置,这里的原来的位置可能和我们想象的不太一样,不是一开始定死的位置,而是经过调整后的位置,因为,这里改变前一个cell对后面的cell是有影响的,应该是后面的y轴位置会自动调整。
原文章是这么说的,但是我在文本内容超过collectionView宽度的时候出现了布局问题,就需要在计算cell宽度的时候加入判断 ,超过时再减去self.minimumInteritemSpacing

还没结束

很多时候UICollectionView并不是单独使用,更多的是嵌套在UITableView的cell中,负责一小部分内容的展示。

那么如何在UITableViewCell中动态调整UICollectionView的大小高度呢?

首先

在UITableViewCell添加UICollectionView并设置约束:
image.png

除了坐标位置外,重点设置UICollectionView的宽,高约束,这边随便设置个值(接近真实大小就行)。

然后

把宽,高的约束通过xib关联起来

@interface MyLiveBroadcastCollectView () <UICollectionViewDataSource, UICollectionViewDelegate>

@property (weak, nonatomic) IBOutlet LiveBroadcastFlowLayout *flowLayout;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *pictureCollectionViewHeightCons;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *pictureCollectionViewWidthCons;

@property (nonatomic, strong) NSArray *dataAry;

@end

通过外部调用方法em_displayWithID:

- (void)em_displayWithID:(id)model{
    self.dataAry = model;
    self.flowLayout.dataArry = model;
    [self reloadData];
    
    //[tableview reload]的时候是异步操作,[UICollectionView reloadData]实际上并没有加载完所有的item
    //所以collectionViewLayout.collectionViewContentSize不准
    //所以就需要重写collectionViewLayout
    CGFloat updateHeight = self.collectionViewLayout.collectionViewContentSize.height;//这里就拿最新撑开的ContentSize去更新高度约束。
    
    self.pictureCollectionViewHeightCons.constant = updateHeight;
    self.pictureCollectionViewWidthCons.constant = self.contentWidth;
}

这边返回的updateHeight是通过重写方法后计算得出的,而self.contentWidth则通过[UIScreen mainScreen].bounds.size.width计算来固定,因为UITableViewCell通过layoutIfNeeded后才能得出UICollectionView的真实宽度,所以暂时只适用于UICollectionView宽度固定的情况。

接下来

通过对UITableViewCell的赋值来完成方法的调用

- (CGFloat)calculateRowHeightWithId:(id)model{
    self.dataModel = model;
    
    if (self.dataModel.height != 0) {
        return self.dataModel.height;
    }
    
    [self em_displayWithID:self.dataModel];
    
    [self layoutIfNeeded];
    
    self.dataModel.height = CGRectGetMaxY(self.stackView.frame);
    return CGRectGetMaxY(self.stackView.frame);
}

- (void)em_displayWithID:(id)model{
    self.dataModel = model;
    /*.省略方法.*/
    [self.tagsCollectionView em_displayWithID:self.dataModel.tagArry];
}

通过调用UITableViewCell的计算高度方法calculateRowHeightWithId,在UITableViewCell的em_displayWithID:方法中对UICollectionView进行赋值方法的调用。

最后,附上效果图的样子:

image.png

参考文章和源码:
AutoLayout下CollectionView的自适应大小

有关iOS UICollectionView的动态布局及内容自适应大小的更多相关文章

  1. ruby-on-rails - 在 Rails 中将文件大小字符串转换为等效千字节 - 2

    我的目标是转换表单输入,例如“100兆字节”或“1GB”,并将其转换为我可以存储在数据库中的文件大小(以千字节为单位)。目前,我有这个:defquota_convert@regex=/([0-9]+)(.*)s/@sizes=%w{kilobytemegabytegigabyte}m=self.quota.match(@regex)if@sizes.include?m[2]eval("self.quota=#{m[1]}.#{m[2]}")endend这有效,但前提是输入是倍数(“gigabytes”,而不是“gigabyte”)并且由于使用了eval看起来疯狂不安全。所以,功能正常,

  2. ruby - 将数组的内容转换为 int - 2

    我需要读入一个包含数字列表的文件。此代码读取文件并将其放入二维数组中。现在我需要获取数组中所有数字的平均值,但我需要将数组的内容更改为int。有什么想法可以将to_i方法放在哪里吗?ClassTerraindefinitializefile_name@input=IO.readlines(file_name)#readinfile@size=@input[0].to_i@land=[@size]x=1whilex 最佳答案 只需将数组映射为整数:@land边注如果你想得到一条线的平均值,你可以这样做:values=@input[x]

  3. ruby-on-rails - 如何在我的 Rails 应用程序 View 中打印 ruby​​ 变量的内容? - 2

    我是一个Rails初学者,但我想从我的RailsView(html.haml文件)中查看Ruby变量的内容。我试图在ruby​​中打印出变量(认为它会在终端中出现),但没有得到任何结果。有什么建议吗?我知道Rails调试器,但更喜欢使用inspect来打印我的变量。 最佳答案 您可以在View中使用puts方法将信息输出到服务器控制台。您应该能够在View中的任何位置使用Haml执行以下操作:-puts@my_variable.inspect 关于ruby-on-rails-如何在我的R

  4. ruby - 查找字符串中的内容类型(数字、日期、时间、字符串等) - 2

    我正在尝试解析一个CSV文件并使用SQL命令自动为其创建一个表。CSV中的第一行给出了列标题。但我需要推断每个列的类型。Ruby中是否有任何函数可以找到每个字段中内容的类型。例如,CSV行:"12012","Test","1233.22","12:21:22","10/10/2009"应该产生像这样的类型['integer','string','float','time','date']谢谢! 最佳答案 require'time'defto_something(str)if(num=Integer(str)rescueFloat(s

  5. ruby - nanoc 和多种布局 - 2

    是否可以为特定(或所有)项目使用多个布局?例如,我有几个项目,我想对其应用两种不同的布局。一个是绿色的,一个是蓝色的(但是)。我想将它们编译到我的输出目录中的两个不同文件夹中(例如v1和v2)。我一直在玩弄规则和编译block,但我不知道这是怎么回事。因为,每个项目在编译过程中只编译一次,我不能告诉nanoc第一次用layout1编译,第二次用layout2编译。我试过这样的东西,但它导致输出文件损坏。compile'*'doifitem.binary?#don’tfilterbinaryitemselsefilter:erblayout'layout1'layout'layout2'

  6. HBase Region 简介和建议数量&大小 - 2

    Region是HBase数据管理的基本单位,region有一点像关系型数据的分区。region中存储这用户的真实数据,而为了管理这些数据,HBase使用了RegionSever来管理region。Region的结构hbaseregion的大小设置默认情况下,每个Table起初只有一个Region,随着数据的不断写入,Region会自动进行拆分。刚拆分时,两个子Region都位于当前的RegionServer,但处于负载均衡的考虑,HMaster有可能会将某个Region转移给其他的RegionServer。RegionSplit时机:当1个region中的某个Store下所有StoreFile

  7. ruby - 如何使用 Selenium Webdriver 根据 div 的内容执行操作? - 2

    我有一个使用SeleniumWebdriver和Nokogiri的Ruby应用程序。我想选择一个类,然后对于那个类对应的每个div,我想根据div的内容执行一个Action。例如,我正在解析以下页面:https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=puppies这是一个搜索结果页面,我正在寻找描述中包含“Adoption”一词的第一个结果。因此机器人应该寻找带有className:"result"的div,对于每个检查它的.descriptiondiv是否包含单词“adoption

  8. ruby-on-rails - Ruby 中意外的大小写行为 - 2

    我在一段非常简单的代码(如我所想)中得到了一个错误的值:org=4caseorgwhenorg=4val='H'endputsval=>nil请不要生气,我希望我错过了一些非常明显的东西,但我真的想不通。谢谢。 最佳答案 这是典型的Ruby错误。case有两种被调用的方法,一种是你传递一个东西作为分支的基础,另一种是你不传递的东西。如果您确实在case中指定了一个表达式语句然后评估所有其他条件并与===进行比较.在这种情况下org评估为false和org===false显然不是真的。所有其他情况也是如此,它们要么是真的,要么是假的。

  9. ruby - 在 Ruby 中动态创建数组 - 2

    有没有办法在Ruby中动态创建数组?例如,假设我想遍历用户输入的书籍数组:books=gets.chomp用户输入:"TheGreatGatsby,CrimeandPunishment,Dracula,Fahrenheit451,PrideandPrejudice,SenseandSensibility,Slaughterhouse-Five,TheAdventuresofHuckleberryFinn"我把它变成一个数组:books_array=books.split(",")现在,对于用户输入的每一本书,我想用Ruby创建一个数组。伪代码来做到这一点:x=0books_array.

  10. ruby - 是否可以将 IRB 提示配置为动态更改? - 2

    我想在IRB中浏览文件系统并让提示更改以反射(reflect)当前工作目录,但我不知道如何在每个命令后进行提示更新。最终,我想在日常工作中更多地使用IRB,让bash溜走。我在我的.irbrc中试过这个:require'fileutils'includeFileUtilsIRB.conf[:PROMPT][:CUSTOM]={:PROMPT_N=>"\e[1m:\e[m",:PROMPT_I=>"\e[1m#{pwd}>\e[m",:PROMPT_S=>"FOO",:PROMPT_C=>"\e[1m#{pwd}>\e[m",:RETURN=>""}IRB.conf[:PROMPT_MO

随机推荐