UICollectionViewCell「居左显示」

准备:

1.UICollectionView Left Aligned Layout

UICollectionView Left Aligned Layout

工程目录:

工程目录

自定义UICollectionViewCell

CollectionViewCell.h 创建UILabel属性,用来传值

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

CollectionViewCell.m 创建显示文本视图

  • 此处titleLB文字大小要和计算的文字大小相同
#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中创建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];
} /**
* 虚拟数据
*/
- (void)initailData{
_allTextArr = @[@"19朵玫瑰礼盒", @"19朵红玫瑰礼盒+百合", @"33朵红玫瑰礼盒", @"33朵红玫瑰礼盒(三世情缘)", @"33朵香槟玫瑰礼盒(生日推荐)", @"38朵红玫瑰心连心礼盒(一见倾心)", @"19朵红玫瑰礼盒(热卖推荐)", @"19朵粉玫瑰礼盒(热卖推荐)", @"19朵混色玫瑰礼盒"];
} /**
* 创建视图
*/
- (void)createMianView{
//居左约束
UICollectionViewLeftAlignedLayout *leftAlignedLayout = [[UICollectionViewLeftAlignedLayout alloc] init];
leftAlignedLayout.minimumLineSpacing = 10; //最小行间距
leftAlignedLayout.minimumInteritemSpacing = 10; //最小列间距
leftAlignedLayout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20); //网格上左下右间距 //网格
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内容
- (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数量
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return _allTextArr.count;
} #pragma mark -UICollectionViewDelegate
//点击item触发方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{ } #pragma mark -UICollectionViewDelegateFlowLayout
//设置每个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);
} //计算文字宽度
- (CGFloat)settingCollectionViewItemWidthBoundingWithText:(NSString *)text{
//1,设置内容大小 其中高度一定要与item一致,宽度度尽量设置大值
CGSize size = CGSizeMake(MAXFLOAT, 20);
//2,设置计算方式
//3,设置字体大小属性 字体大小必须要与label设置的字体大小一致
NSDictionary *attributeDic = @{NSFontAttributeName: [UIFont systemFontOfSize:14]};
CGRect frame = [text boundingRectWithSize:size options: NSStringDrawingUsesLineFragmentOrigin attributes:attributeDic context:nil];
//4.添加左右间距
return frame.size.width + 15;
}

运行效果:

效果图

UICollectionViewCell「居左显示」的更多相关文章

  1. 使Gallery时设置居左显示

    Gallery中的图片默认是居中显示的.可是在非常多情况下我们须要它居左显示,这样做有一个简单方法.就是把Gallery的left设置为负多少,如以下的方法: Drawable drawable=ca ...

  2. UIButton文字居左显示

    题外话:时间依然过的非常快.不知不觉2015年就过去一半了.感觉自己好像没有大的改变.仅仅能感叹时间飞逝,却不能有所收获. 我从来都不是一个安于现状的人,改变自己的想法从未停止过.我想大多数人都跟我有 ...

  3. DevExpress相关控件中非字符数值居左显示

    用了这么长时间的DevExpress控件,今天遇到俩问题. 一个是从头到尾看了一遍编译成功的例子,只能感慨,功能太丰富了,自己所用的不过是冰山一角.有些自己一直想实现的效果,原来早就有现成的可用,汗颜 ...

  4. 谈谈一些有趣的CSS题目(五)-- 单行居中,两行居左,超过两行省略

    开本系列,讨论一些有趣的 CSS 题目,抛开实用性而言,一些题目为了拓宽一下解决问题的思路,此外,涉及一些容易忽视的 CSS 细节. 解题不考虑兼容性,题目天马行空,想到什么说什么,如果解题中有你感觉 ...

  5. css实现一行居中显示,两行靠左显示,超过两行以引号省略

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. css设置图片居中、居左、居右

      CreateTime--2017年12月8日14:25:09 Author:Marydon css设置图片居中.居左.居右 图片一般默认是居左显示的,如何更改它的水平位置呢? <div st ...

  7. 谈谈一些有趣的CSS题目-- 单行居中,两行居左,超过两行省略

    开本系列,讨论一些有趣的 CSS 题目,抛开实用性而言,一些题目为了拓宽一下解决问题的思路,此外,涉及一些容易忽视的 CSS 细节. 解题不考虑兼容性,题目天马行空,想到什么说什么,如果解题中有你感觉 ...

  8. pageControl设置不居中显示,居左或居右

    UIPageControl控件,默认是居中显示的,如下图: 在很多的APP中,会看到pageControl是居左或居右显示的,如下图:   如何控制pageControl的位置显示呢? 设置为居右的代 ...

  9. 使用注册表优化终端、编辑器的中英字体混合显示,如「Consolas + 雅黑」「Monaco + 雅黑」

    在终端.cmd.编辑器中偶尔会有中文字符出现,Windows下默认的点阵字体「宋体」和等宽英文字符放在一起非常违和.一个解决方法是下载混合字体,比如「Consolas + YAHEI hybrid」, ...

随机推荐

  1. avalon实现分页组件

    前言 分页组件比较常见,但是用avalon实现的见的不多,这个分页组件,可以适配2种分页方式, 第一种是每次点击下一页,就请求一次后台,并返回当页数据和总条数,我称之为假分页: 第二种是一次性把所有数 ...

  2. 《Unity系列》Json文件格式的解析——初级教程

    Json作为轻量级的数据交换格式,被广泛应用于网络数据传输中.相关的解析工具层出不穷,一般掌握一个工具的应用其他的相应工具就能立马学会. 这里以C#中的LitJson为例给大家示范一下解析工具的用法. ...

  3. linux安装git、node、pm2

    一.安装Git 下载:# wget https://www.kernel.org/pub/software/scm/git/git-2.9.5.tar.gz 解压:# tar -zxvf git-2. ...

  4. 重构指南 - 使用多态代替条件判断(Replace conditional with Polymorphism)

    多态(polymorphism)是面向对象的重要特性,简单可理解为:一个接口,多种实现. 当你的代码中存在通过不同的类型执行不同的操作,包含大量if else或者switch语句时,就可以考虑进行重构 ...

  5. 从零开始的全栈工程师——PHP篇 ( 单词汇总 ) ( php解决文字乱码 )

    解决乱码: header("Content-Type: text/html;charset=utf-8"); 单词 局部的: local 全局的: global 静态的: stat ...

  6. 【转】 ASP.NET使用ICallbackEventHandler无刷新验证用户名是否可用

    功能说明:当用户在用户名输入框输入字符并焦点离开此输入框时,自动到数据库用户表中验证此用户名是否已被注册,如果已被注册,显示[不可用],反之,显示[可用],期间页面不刷新,读者也可以考虑将提示文字换成 ...

  7. 2016微软技术大会Azure相关回顾

    3 天的时间稍纵即逝,伴随着本届大会压轴大奖的揭晓,2016 年度的微软技术大会完美落幕.以“数字化转型”为主题,来自微软全球的近百位顶尖技术专家.工程师和业务负责人拔冗而至,在 130 余场的专业技 ...

  8. 定时备份SQL Server数据库

    一.手动备份: 1.整个数据库备份:选择数据库 => 右键任务 => 备份: 2.导出一张表的框架:选择表 => 编写表脚本为 => CREATE到: 3.导出一张表的数据:  ...

  9. [Err] 1214 - The used table type doesn't support FULLTEXT indexes

    -- -- Table structure for table `film_text` -- -- InnoDB added FULLTEXT support in 5.6.10. If you us ...

  10. 使用WindowsService为宿主实装WCF 服务

    1. 写WCF服务   创建一个接口类,与一个实现类.接口类上打上[ServiceContract]标签,需要暴露的服务方法上打上[OperationContract] (注意:增加System.Se ...