iOS-UICollectionView一行可以居左或居中,居右自适应宽度显示多个Item

近来需要做一个需求,就是可以输入新建多个标签,标签高度固定,但宽度不固定且个数不固定,效果图如下:

因此初步设想:使用UICollectionView的item自适应宽度,方法如下:

#import "TestCollectionCell.h"

@interface TestCollectionCell()

@property(nonatomic,weak)UILabel *yardLab;

@end

@implementation TestCollectionCell

- (instancetype) initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setSubView];
    }
    return self;
}

- (void)setSubView{
    CGFloat labH = 30.0;
    UILabel *yardLab = [[UILabel alloc] init];
    yardLab.textAlignment = NSTextAlignmentCenter;
    self.yardLab = yardLab;
    yardLab.layer.masksToBounds = YES;
    yardLab.layer.cornerRadius = labH/2.0;
    yardLab.backgroundColor = YardsColor;
//    yardLab.text = @"22222222343423";
    yardLab.font = SET_FONT(14.0);
    [self.contentView addSubview:yardLab];
    [yardLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self.contentView);
        make.height.mas_equalTo(@(labH));
    }];
    yardLab.userInteractionEnabled = NO;
    //删除按钮用自身
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(singleTap:)];
    [tapGestureRecognizer setNumberOfTapsRequired:1];
    [yardLab addGestureRecognizer:tapGestureRecognizer];
    
    CGFloat btnH = 18.0;
    UIButton *deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    deleteBtn.backgroundColor = [UIColor redColor];
    self.deleteBtn = deleteBtn;
    deleteBtn.hidden = YES;
    deleteBtn.enabled = NO;
    deleteBtn.layer.masksToBounds = YES;
    deleteBtn.layer.cornerRadius = btnH/2.0;
    [deleteBtn setBackgroundImage:[UIImage imageNamed:@"yard_delete"] forState:UIControlStateNormal];
//    [deleteBtn addTarget:self action:@selector(managerAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:deleteBtn];
    [deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(@(btnH));
        make.right.equalTo(yardLab.mas_right).offset(btnH/3.0);
        make.top.mas_equalTo(@(-btnH/2.0));
    }];
}

#pragma mark — 实现自适应文字宽度的关键步骤:item的layoutAttributes
- (UICollectionViewLayoutAttributes *)preferredLayoutAttributesFittingAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes {
    [self setNeedsLayout];
    [self layoutIfNeeded];
    CGSize size = [self.contentView systemLayoutSizeFittingSize:layoutAttributes.size];
    CGRect cellFrame = layoutAttributes.frame;
    cellFrame.size.width = size.width + 16;
    layoutAttributes.frame = cellFrame;
    return layoutAttributes;
}

过程发现两个问题:

1、如果使用button,如下方法不能计算出宽度,要使用UILabel

2、如果只有一个item,会发现居中显示,但是并未发现UICollectionView有类似UILabel居左,居中,居右显示item的属性方法,效果图如下:

解决办法如下:

    //collectionView的item只剩下一个时自动左对齐
    SEL sel = NSSelectorFromString(@"_setRowAlignmentsOptions:");
    if ([yardsCollection.collectionViewLayout respondsToSelector:sel]) {
        ((void(*)(id,SEL,NSDictionary*)) objc_msgSend)(yardsCollection.collectionViewLayout,sel, @{@"UIFlowLayoutCommonRowHorizontalAlignmentKey":@(NSTextAlignmentLeft),@"UIFlowLayoutLastRowHorizontalAlignmentKey" : @(NSTextAlignmentLeft),@"UIFlowLayoutRowVerticalAlignmentKey" : @(NSTextAlignmentCenter)});
    }

最终效果:

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值