实现UICollectionViewCell自适应文字宽度和选中项目

1.collectionView

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

    for (AutoCellModel * model in self.dataArray) {
        model.isSelected = NO;
    }
    AutoCellModel *model = self.dataArray[indexPath.item];
    model.isSelected = YES;
    _selectModel = model;
    [self.collectionView reloadData];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    AutoCellModel *model = self.dataArray[indexPath.item];

    AutoTagViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.contentTextLabel.text = model.colorText;
    cell.layer.cornerRadius=5;
    cell.layer.masksToBounds=YES;
    if (model.isSelected) {

    cell.backgroundColor = [UIColor orangeColor];
    cell.contentTextLabel.textColor = [UIColor whiteColor];
    }else{
    cell.backgroundColor = DefaultColor;
    cell.contentTextLabel.textColor = [UIColor blackColor];   
    }   
    return cell;
}
//实现宽度
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    AutoCellModel *model = self.dataArray[indexPath.row];
    HWDLog(@"%@",model.colorText);
    CGFloat width = [model.colorText stringWidthWithFont:[UIFont systemFontOfSize:14] height: 1000];
    return CGSizeMake(width + 15 * 2, 30);
}
//*******//
- (CGFloat)stringWidthWithFont:(UIFont *)font height:(CGFloat)height {
    if (self == nil || self.length == 0) {
        return 0;
    }
    NSString *copyString = [NSString stringWithFormat:@"%@", self];
    CGSize constrainedSize = CGSizeMake(999999, height);
    NSDictionary * attribute = @{NSFontAttributeName:font};
    CGSize size = [copyString boundingRectWithSize:constrainedSize options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute context:nil].size;
    return ceilf(size.width);
}

11.数据模型

#import <Foundation/Foundation.h>
@interface AutoCellModel : NSObject
@property (nonatomic,copy) NSString *colorText;
@property (nonatomic,assign) BOOL isSelected;
@property (nonatomic,assign) NSInteger productId;
@property (nonatomic,assign) NSInteger carId;
@end

2.自定义cell

#import <UIKit/UIKit.h>

static CGFloat const shadowHeight = 20;
static CGFloat const marginLabelToCell = 15;
static CGFloat const labelHeight = 17;

@interface AutoTagViewCell : UICollectionViewCell
@property (nonatomic,strong) UILabel *contentTextLabel;
@property (nonatomic,strong) UIImageView *backgroundImgView;
@property (nonatomic,assign) BOOL isSelected;
@end
//
//  AutoTagViewCell.m
//  Created by 宇航 on 17/4/24.
//  Copyright © 2017年 HWD. All rights reserved.
//

#import "AutoTagViewCell.h"
#import "HWDConstant.h"

@implementation AutoTagViewCell
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        _contentTextLabel = [[UILabel alloc] init];
        self.backgroundImgView = [[UIImageView alloc] init];
//        self.backgroundImgView.image = [UIImage imageNamed:@"tag_bg"];
        [self.contentView addSubview:self.backgroundImgView];
        [self.contentView addSubview:_contentTextLabel];
        self.contentTextLabel.textAlignment = NSTextAlignmentCenter;
        self.contentTextLabel.backgroundColor = [UIColor clearColor];
        self.contentTextLabel.font = [UIFont systemFontOfSize:14];
        self.contentTextLabel.textColor = HWDColor(92, 92, 92);
        self.isSelected = NO;
        [self.contentView addSubview:self.contentTextLabel];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    CGRect frame = self.bounds;
    _contentTextLabel.frame = CGRectMake(marginLabelToCell, (frame.size.height - shadowHeight - labelHeight) / 2, frame.size.width - marginLabelToCell * 2, labelHeight);
    CGPoint center = _contentTextLabel.center;
    center.y = frame.size.height / 2.2;
    _contentTextLabel.center = center;
    _backgroundImgView.frame = self.contentView.bounds;
}

@end
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值