UICollectionViewCell “show left”

Get ready:

1.UICollectionView Left Aligned Layout
  • A UICollectionView in the left shows the constraints
    , click download _UICollectionView Left Aligned Layout
UICollectionViewCell ,
, UICollectionView, Left, Aligned, Layout

Engineering catalogue:

UICollectionViewCell
project directory

Custom UICollectionViewCell

CollectionViewCell.h creates the UILabel property for passing values

#import < UIKit/UIKit.h> @interface CollectionViewCell: UICollectionViewCell, @property (nonatomic, strong) UILabel, *titleLB;; @end

CollectionViewCell.m creates a display text view

  • Here, the size of the titleLB literal is the same as the literal size of the calculation
#import "CollectionViewCell.h" @implementation CollectionViewCell (instancetype) initWithFrame: (CGRect) frame{self = [super initWithFrame:frame]; if (self) self.backgroundColor [UIColor colorWithRed:251/255.0 green:74/255.0 blue:71/ {alpha:1] = 255; self.layer.cornerRadius = 3; self.layer.masksToBounds = YES; self.layer.borderColor = [UIColor lightTextColor].CGColor; self.layer.borderWidth = 0.5; [self createSubViews];} return self;} - (void) createSubViews{_titleLB = [[UILabel alloc] initWithFrame:CGRectMake (0, 0, self.frame.size.width, self.frame.size.height); //_titleLB.backgroundColor [UIColor = yellowColor]; _titleLB.textColor = [UIColor whiteColor]; _titleLB.textAlignment = NSTextAlignmentCenter; _titleLB.font = [UIFont systemFontOfSize:14]; [self.contentView addSubview:_titleLB];} @end

Creating UICollectionView in ViewController

ViewController.h

#import < UIKit/UIKit.h> @interface ViewController: UIViewController @end

ViewController.m

#import "ViewController.h" #import "UICollectionViewLeftAlignedLayout.h" #import "CollectionViewCell.h" @interface (ViewController) < UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout> @ end @implementation ViewController{NSArray; *_allTextArr;} - {(void) viewDidLoad [super viewDidLoad] any additional setup after; / / Do loading the view, typically from a nib. self.view.backgroundColor = [UIColor whiteColor]; [self initailData; [self createMianView];} / * * * * / virtual data - (void) initailData{_allTextArr = @[@ "19 roses gift", "@ 19 red roses gift + lily", "33 red roses @ @ gift", "33 red roses gift (Love III)", "@ 33 flower champagne roses gift box push (birthday Recommended) "@" 38 red roses heart gift (at first sight) "@" 19 red roses gift box (hot recommendation) "@" 19 pink roses gift (hot recommended) "@" 19 flower color rose gift "];} / * * * * / (void - create view createMianView{) / left UICollectionViewLeftAlignedLayout *leftAlignedLayout = [[UICollectionViewLeftAlignedLayout alloc] init] constraint; leftAlignedLayout.minimumLineSpacing = 10; / / the minimum line spacing leftAlignedLayout.minimumInteritemSpacing = 10; / / the minimum column spacing leftAlignedLayout.sectionInset = UIEdgeInsetsMake (20, 20, 20, 20); / / grid left and right spacing grid UICollectionView [[UICollectionView alloc] / / *mainCollectionView = initWithFrame:CGRectMake (0, 0, self.view.fra Me.size.width, self.view.frame.size.height collectionViewLayout:leftAlignedLayout]); mainCollectionView.backgroundColor [UIColor = whiteColor]; mainCollectionView.dataSource = self; mainCollectionView.delegate = self; [self.view addSubview: mainCollectionView]; [mainCollectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@ "cell"];} - {(void) didReceiveMemoryWarning [super didReceiveMemoryWarning] of any resources; / / Dispose that can be recreated. #pragma mark -UICollectionViewDataSource //item} - (UICollectionViewCell *) (collectionView: UICollectionView collectionView cellForItemAtIndexPath: (NSIndexPath) * *) indexPath{CollectionViewCell = *cell (CollectionViewCell * [colle) CtionView dequeueReusableCellWithReuseIdentifier:@ "cell" forIndexPath:indexPath]; cell.titleLB.text = [NSString stringWithFormat:@, [_allTextArr objectAtIndex:indexPath.row]] "% @"; return cell;} //Section number item (NSInteger) - collectionView: (UICollectionView *) collectionView numberOfItemsInSection: (NSInteger) section{return _allTextArr.count #pragma mark -UICollectionViewDelegate;} / / item - click the trigger method (void) collectionView: (UICollectionView *) collectionView didSelectItemAtIndexPath: (NSIndexPath *) indexPath{#pragma mark -UICollectionViewDelegateFlowLayout} / / set for each size of item - (CGSize) collectionView: (UICollectionView * collectionView) layout: (UICollectionViewLayout *) collectionViewLayout sizeForItemAtIn DexPath: (NSIndexPath *) indexPath{NSString *textString = [_allTextArr objectAtIndex:indexPath.row]; CGFloat cell_width = [self settingCollectionViewItemWidthBoundingWithText:textString]; return CGSizeMake (cell_width, 30);} / / text width calculation - (CGFloat) settingCollectionViewItemWidthBoundingWithText: (NSString *) text{//1, set the content size which height must be consistent with the item, the width of try to set up a large value CGSize size = CGSizeMake (MAXFLOAT, 20); //2, set the //3 calculation, set the font size and font size attribute must set the font size of label NSDictionary *attributeDic [UIFont systemFontOfSize:14]} = @{NSFontAttributeName:; CGRect = frame [text boundingRectWithSize:size options: NSStringDrawin GUsesLineFragmentOrigin attributes:attributeDic context:nil]; //4. add left and right spacing return frame.size.width + 15}

Operation effect:

UICollectionViewCell
renderings