#pragma mark â èªå®ä¹cell
#import "SelfSizingCollectCell.h"
#import "Masonry.h"
#define itemHeight 60
@implementation SelfSizingCollectCell
- (instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
self.contentView.backgroundColor = [UIColor redColor];
// ç¨çº¦ææ¥åå§åæ§ä»¶:
self.textLabel = [[UILabel alloc] init];
self.textLabel.textAlignment =NSTextAlignmentCenter;
self.textLabel.backgroundColor = [UIColor greenColor];
[self.contentView addSubview:self.textLabel];
#pragma mark â å¦æ使ç¨CGRectMakeæ¥å¸å±,æ¯éè¦å¨preferredLayoutAttributesFittingAttributesæ¹æ³ä¸å»ä¿®æ¹textlabelçframeç
// self.textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, 30)];
#pragma mark â å¦æ使ç¨çº¦ææ¥å¸å±,åæ éå¨preferredLayoutAttributesFittingAttributesæ¹æ³ä¸å»ä¿®æ¹cellä¸çåæ§ä»¶lçframe
[self.textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
// make 代表约æ:
make.top.equalTo(self.contentView).with.offset(0); // 对å½åviewçtopè¿è¡çº¦æ,è·ç¦»åç
§viewçä¸è¾¹çæ¯ :
make.left.equalTo(self.contentView).with.offset(0); // 对å½åviewçleftè¿è¡çº¦æ,è·ç¦»åç
§viewç左边çæ¯ :
make.height.equalTo(@(itemHeight/2)); // é«åº¦
make.right.equalTo(self.contentView).with.offset(0); // 对å½åviewçrightè¿è¡çº¦æ,è·ç¦»åç
§viewçå³è¾¹çæ¯ :
}];
}
return self;
}
#pragma mark â å®ç°èªéåºæå宽度çå
³é®æ¥éª¤:itemçlayoutAttributes
- (UICollectionViewLayoutAttributes *)preferredLayoutAttributesFittingAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes{
UICollectionViewLayoutAttributes *attributes = [super preferredLayoutAttributesFittingAttributes:layoutAttributes];
CGRect rect = [self.textLabel.text boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, itemHeight) options:NSStringDrawingTruncatesLastVisibleLine| NSStringDrawingUsesFontLeading |NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:14]} context:nil];
rect.size.width +=8;
rect.size.height+=8;
attributes.frame = rect;
return attributes;
}
@end
#pragma mark â è§å¾æ§å¶å¨ä¸ä½¿ç¨:(å
³é®)
layout.estimatedItemSize = CGSizeMake(20, 60); // layout约æè¿è¾¹å¿
é¡»è¦ç¨estimatedItemSizeæè½å®ç°èªéåº,使ç¨itemSzieæ æ
è§å¾æ§å¶å¨.mä¸æºç
#import "ViewController.h"
#import "SelfSizingCollectCell.h"
@interface ViewController () <UICollectionViewDelegate,UICollectionViewDataSource>
@property (strong, nonatomic) UICollectionView *collection;
@property (strong, nonatomic) NSArray *dataArr;
@end
@implementation ViewController
#pragma mark --- lazyinit
- (NSArray *)dataArr{
if (!_dataArr) {
_dataArr = [NSArray array];
}
return _dataArr;
}
- (void)viewDidLoad {
[super viewDidLoad];
NSString *text = @"The UICollectionViewFlowLayout class is a concrete layout object that organizes items into a grid with optional header and footer views for each section. The items in the collection view flow from one row or column (depending on the scrolling direction) to the next, with each row comprising as many cells as will fit. Cells can be the same sizes or different sizesThe UICollectionViewFlowLayout class is a concrete layout object that organizes items into a grid with optional header and footer views for each section. The items in the collection view flow from one row or column.";
self.dataArr = [text componentsSeparatedByString:@" "];
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
// 设置å
·ä½å±æ§
// 1.设置 æå°è¡é´è·
layout.minimumLineSpacing = 20;
// 2.设置 æå°åé´è·
layout. minimumInteritemSpacing = 10;
// 3.设置itemåçå¤§å° (å¯ä»¥ç¨äºèªéåº)
layout.estimatedItemSize = CGSizeMake(20, 60);
// 设置æ»å¨çæ¹å (é»è®¤æ¯ç«çæ»å¨ç)
layout.scrollDirection = UICollectionViewScrollDirectionVertical;
// 设置itemçå
è¾¹è·
layout.sectionInset = UIEdgeInsetsMake(10,10,10,10);
self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) collectionViewLayout:layout];
self.collection.backgroundColor = [UIColor whiteColor];
self.collection.delegate = self;
self.collection.dataSource = self;
[self.view addSubview:self.collection];
[self.collection registerClass:[SelfSizingCollectCell class] forCellWithReuseIdentifier:@"SelfSizingCollectCell"];
}
#pragma MARK --- UICollectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return [self.dataArr count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
SelfSizingCollectCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SelfSizingCollectCell" forIndexPath:indexPath];
cell.textLabel.text = self.dataArr[indexPath.row];
return cell;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end