ios - 多行容器 View 根据宽度布置子项

标签 ios objective-c custom-controls

基本上我正在尝试为下面描述的自定义 View 找到一个 iOS 库(首选 Obj-C)。我找不到类似的东西,但看起来这仍然是很普遍的观点,所以社区中的任何人都可以指出我正确的地方。

所以我试图在 iOS 中实现一个 View 来复制图像的行为:

enter image description here

基本上它是一个水平容器 View ,它根据宽度堆叠其他 View (基本上是 UILabel),并在需要时动态添加更多行。

所以我目前的高层次方法是按如下方式实现它:

  1. 将 NSString 列表传递到容器 View
  2. 容器 View 将为每个字符串创建 UILabel
  3. 然后计算每个标签的宽度和所有标签的总宽度
  4. 容器 View 根据容器的宽度动态计算当前行的项目数。
  5. 其余项目进入下一行(容器高度增加)并且当队列中有未处理的 UILabel 时重复第 4 步。

虽然这个过程相当简单,但我仍在努力寻找可能的方法来简化开发并节省客户在此功能上的预算。

所以也许有人可以指出更好的方法?似乎 UICollectionView 是一个不错的选择,但也许仍然有任何库可以执行与我上面描述的类似的操作?

在 github 上找不到任何东西,但可能只是因为我搜索不正确。

最佳答案

代码工作

    - (IBAction)actionTagCancel:(UIButton *)sender {
    [arrTagList removeObjectAtIndex:sender.tag];
    [self.collecTagCategory reloadData];

}


#pragma mark- collection data source

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return arrTagList.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {


    CGSize tagLabelSize = [arrTagList[indexPath.row] boundingRectWithSize: CGSizeMake(self.view.frame.size.width-75, 120)
                                                              options:NSStringDrawingUsesLineFragmentOrigin
                                                           attributes:@{NSFontAttributeName:[UIFont fontWithName:@"Helvetica Neue" size:14.0 ]}
                                                              context:nil].size;

    return CGSizeMake (tagLabelSize.width + 45, 30);
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    TagViewListCell *tagCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"tagCell" forIndexPath:indexPath];
    tagCell.viewTag.layer.cornerRadius = 4;

    tagCell.btnTagCancel.tag = indexPath.row;

    tagCell.lblTagName.text = arrTagList[indexPath.row];
    _constTagViewHeight.constant = _collecTagCategory.contentSize.height;
    return tagCell;

}

输出

enter image description here

删除标签后自动管理

enter image description here

Edit

Get Repository of project from here

关于ios - 多行容器 View 根据宽度布置子项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42279044/

相关文章:

python - Pygame - 如何让我的用户更改他们的输入键? (自定义键绑定(bind))

javascript - 使用 HTML5/JQuery 播放视频

ios - 查找当前事件的 View Controller

ios - 从侧边栏菜单在 UIWebView 中加载错误的 url

objective-c - 在 objective-c 中初始化和写入数组

ios - 在 Objective C/ios 中本地保存文件

ios - 如何正确更新自定义类属性?

ios - UICollectionView io11 - 顶部插图

iphone - 如果设备已经在播放音乐,如何防止我的应用播放声音

ios - 裁剪和调整从图库或相机中选择的图像