UICollectionViewCell「居左顯示」

準(zhǔn)備:

1.UICollectionView Left Aligned Layout
UICollectionView Left Aligned Layout

工程目錄:

工程目錄

自定義UICollectionViewCell

CollectionViewCell.h 創(chuàng)建UILabel屬性,用來傳值

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

CollectionViewCell.m 創(chuàng)建顯示文本視圖

  • 此處titleLB文字大小要和計(jì)算的文字大小相同
#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/255.0 alpha:1];
        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

在ViewController中創(chuàng)建UICollectionView

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];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor whiteColor];
    [self initailData];
    [self createMianView];
}

/**
 *  虛擬數(shù)據(jù)
 */
- (void)initailData{
    _allTextArr = @[@"19朵玫瑰禮盒", @"19朵紅玫瑰禮盒+百合", @"33朵紅玫瑰禮盒", @"33朵紅玫瑰禮盒(三世情緣)", @"33朵香檳玫瑰禮盒(生日推薦)", @"38朵紅玫瑰心連心禮盒(一見傾心)", @"19朵紅玫瑰禮盒(熱賣推薦)", @"19朵粉玫瑰禮盒(熱賣推薦)", @"19朵混色玫瑰禮盒"];
}

/**
 *  創(chuàng)建視圖
 */
- (void)createMianView{
    //居左約束
     UICollectionViewLeftAlignedLayout *leftAlignedLayout = [[UICollectionViewLeftAlignedLayout alloc] init];
    leftAlignedLayout.minimumLineSpacing = 10;                          //最小行間距
    leftAlignedLayout.minimumInteritemSpacing = 10;                     //最小列間距
    leftAlignedLayout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);  //網(wǎng)格上左下右間距
    
    //網(wǎng)格
    UICollectionView *mainCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.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];
    // Dispose of any resources that can be recreated.
}

#pragma mark -UICollectionViewDataSource
//item內(nèi)容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CollectionViewCell *cell = (CollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.titleLB.text = [NSString stringWithFormat:@"%@", [_allTextArr objectAtIndex:indexPath.row]];
    return cell;
}

//Section中item數(shù)量
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _allTextArr.count;
}

#pragma mark -UICollectionViewDelegate
//點(diǎn)擊item觸發(fā)方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
}

#pragma mark -UICollectionViewDelegateFlowLayout
//設(shè)置每個(gè)item的尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    NSString *textString = [_allTextArr objectAtIndex:indexPath.row];
    CGFloat cell_width = [self settingCollectionViewItemWidthBoundingWithText:textString];
    return CGSizeMake(cell_width, 30);
}

//計(jì)算文字寬度
- (CGFloat)settingCollectionViewItemWidthBoundingWithText:(NSString *)text{
    //1,設(shè)置內(nèi)容大小  其中高度一定要與item一致,寬度度盡量設(shè)置大值
    CGSize size = CGSizeMake(MAXFLOAT, 20);
    //2,設(shè)置計(jì)算方式
    //3,設(shè)置字體大小屬性   字體大小必須要與label設(shè)置的字體大小一致
    NSDictionary *attributeDic = @{NSFontAttributeName: [UIFont systemFontOfSize:14]};
    CGRect frame = [text boundingRectWithSize:size options: NSStringDrawingUsesLineFragmentOrigin attributes:attributeDic context:nil];
    //4.添加左右間距
    return frame.size.width + 15;
}

運(yùn)行效果:

效果圖
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子骤素,更是在濱河造成了極大的恐慌,老刑警劉巖埠啃,帶你破解...
    沈念sama閱讀 217,509評論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異畦攘,居然都是意外死亡霸妹,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,806評論 3 394
  • 文/潘曉璐 我一進(jìn)店門知押,熙熙樓的掌柜王于貴愁眉苦臉地迎上來叹螟,“玉大人鹃骂,你說我怎么就攤上這事“照溃” “怎么了畏线?”我有些...
    開封第一講書人閱讀 163,875評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長良价。 經(jīng)常有香客問我寝殴,道長,這世上最難降的妖魔是什么明垢? 我笑而不...
    開封第一講書人閱讀 58,441評論 1 293
  • 正文 為了忘掉前任蚣常,我火速辦了婚禮,結(jié)果婚禮上痊银,老公的妹妹穿的比我還像新娘抵蚊。我一直安慰自己,他們只是感情好溯革,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,488評論 6 392
  • 文/花漫 我一把揭開白布贞绳。 她就那樣靜靜地躺著,像睡著了一般致稀。 火紅的嫁衣襯著肌膚如雪冈闭。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,365評論 1 302
  • 那天抖单,我揣著相機(jī)與錄音萎攒,去河邊找鬼。 笑死臭猜,一個(gè)胖子當(dāng)著我的面吹牛躺酒,可吹牛的內(nèi)容都是我干的押蚤。 我是一名探鬼主播蔑歌,決...
    沈念sama閱讀 40,190評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼揽碘!你這毒婦竟也來了次屠?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,062評論 0 276
  • 序言:老撾萬榮一對情侶失蹤雳刺,失蹤者是張志新(化名)和其女友劉穎劫灶,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體掖桦,經(jīng)...
    沈念sama閱讀 45,500評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡本昏,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,706評論 3 335
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了枪汪。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片涌穆。...
    茶點(diǎn)故事閱讀 39,834評論 1 347
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡怔昨,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出宿稀,到底是詐尸還是另有隱情趁舀,我是刑警寧澤,帶...
    沈念sama閱讀 35,559評論 5 345
  • 正文 年R本政府宣布祝沸,位于F島的核電站矮烹,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏罩锐。R本人自食惡果不足惜奉狈,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,167評論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望涩惑。 院中可真熱鬧嘹吨,春花似錦、人聲如沸境氢。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,779評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽萍聊。三九已至问芬,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間寿桨,已是汗流浹背此衅。 一陣腳步聲響...
    開封第一講書人閱讀 32,912評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留亭螟,地道東北人挡鞍。 一個(gè)月前我還...
    沈念sama閱讀 47,958評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像预烙,于是被迫代替她去往敵國和親墨微。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,779評論 2 354

推薦閱讀更多精彩內(nèi)容