美文网首页
UICollectionViewCell 宽度自适应/自动居左

UICollectionViewCell 宽度自适应/自动居左

作者: hie | 来源:发表于2019-10-30 09:48 被阅读0次
    @implementation PolyCell
    
    - (instancetype)initWithFrame:(CGRect)frame{
        self = [super initWithFrame:frame];
        if (self) {
            self.titleLabel = [[UILabel alloc] init];
            self.titleLabel.textAlignment =NSTextAlignmentCenter;
            self.titleLabel.textColor = color333333;
            self.titleLabel.backgroundColor = colorE3E3E3;
            self.titleLabel.font = kFont(13);
            self.titleLabel.cornerRadius = 5;
            [self.contentView addSubview:self.titleLabel];
            
            [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.equalTo(self);
            }];
        }
        return self;
    }
    
    #pragma mark — 实现自适应文字宽度的关键步骤:item的layoutAttributes
    - (UICollectionViewLayoutAttributes *)preferredLayoutAttributesFittingAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes{
        
        UICollectionViewLayoutAttributes *attributes = [super preferredLayoutAttributesFittingAttributes:layoutAttributes];
        CGRect rect = [self.titleLabel.text boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, cellHeight) options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesFontLeading |NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: self.titleLabel.font} context:nil];
        rect.size.width +=10;
        rect.size.height+=10;
        attributes.frame = rect;
        return attributes;
    }
    @end
    
    - (UICollectionView *)collectionView{
        if (!_collectionView) {
            UICollectionViewFlowLayout  *layout = [[UICollectionViewFlowLayout alloc] init];
            layout.minimumLineSpacing = 10;
            layout. minimumInteritemSpacing  = 10;
            layout.scrollDirection =  UICollectionViewScrollDirectionVertical;
    
            // 设置item预估的大小
            layout.estimatedItemSize = CGSizeMake(20, cellHeight);
            
            _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) collectionViewLayout:layout];
            _collectionView.backgroundColor = [UIColor whiteColor];
            _collectionView.delegate = self;
            _collectionView.dataSource = self;
            [_collectionView registerClass:[PolyCell class] forCellWithReuseIdentifier:@"PolyCell"];
            
            //collectionView的item只剩下一个时自动左对齐
            SEL sel = NSSelectorFromString(@"_setRowAlignmentsOptions:");
            if ([_collectionView.collectionViewLayout respondsToSelector:sel]) {
                ((void(*)(id,SEL,NSDictionary*)) objc_msgSend)(_collectionView.collectionViewLayout,sel, @{@"UIFlowLayoutCommonRowHorizontalAlignmentKey":@(NSTextAlignmentLeft),@"UIFlowLayoutLastRowHorizontalAlignmentKey" : @(NSTextAlignmentLeft),@"UIFlowLayoutRowVerticalAlignmentKey" : @(NSTextAlignmentCenter)});
            }
            
        }
        return _collectionView;
    }
    
    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
        
        PolyCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PolyCell" forIndexPath:indexPath];
        cell.titleLabel.text = self.tags[indexPath.row];
        return cell;
    }
    

    相关文章

      网友评论

          本文标题:UICollectionViewCell 宽度自适应/自动居左

          本文链接:https://www.haomeiwen.com/subject/czznvctx.html