您的位置:首页 > 产品设计 > UI/UE

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

2017-12-21 15:37 323 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: