实现UICollectionViewCell自适应文字宽度和选中项目
2017-12-21 15:37
323 查看
1.collectionView
11.数据模型
2.自定义cell
- (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
相关文章推荐
- 进击的KFC: OC实现UICollectionViewCell自适应文字宽度
- iOS项目开发实战——实现UICollectionView的动态增加Cell与Section
- Swift 使用SnapKit实现 UICollectionViewCell 高度自适应
- UICollectionView 不同大小Cell 等间距的实现
- QTableView根据标题文字和表格文字自适应宽度 QTableView m_pTable;
- ios之UITableView批量选中cell并实现删除cell的简单方法
- iOS - UICollectionView 简单拖动 cell 实现
- UICollectionViewCell水平缩放,中间大两边小效果的实现
- ios 文字 控件 自适应 高度 宽度 CustomCell 自适应高度+uilabel自动换行+ UITextView 根据内容自动调整高度
- 关于UICollectionView横向分页滚动,cell左右排版功能的实现
- iOS tableview 选中Cell后的背景颜色和文字颜色
- AJ学IOS 之微博项目实战(11)发送微博自定义TextView实现带占位文字
- 猫猫学iOS 之微博项目实战(11)发送微博自定义TextView实现带占位文字
- TableView中如何实现点中cell的文字变为红色,其他cell的文字是黑色
- UICollectionView高度宽度自适应缓存框架
- 解决iOS中UICollectionViewCell点击无反应,长按才能选中的bug
- IOS UItableview UIcollectionview cell高度自适应
- 根据展示文字自适应 cell 高度,实现点击cell的伸缩扩展
- QTableView根据标题文字和表格文字自适应宽度 QTableView m_pTable;