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. JavaScript 监听回车事件

    JS监听某个输入框 //回车事件绑定 $('#search_input').bind('keyup', function(event) { if (event.keyCode == "13& ...

  2. linq之Capacity(转载)

    出处:博客园 作者:mumuliang 连接:http://www.cnblogs.com/mumuliang/p/3914425.html Capacity 在.NET中List的容量应该只是受到硬 ...

  3. Mysql只Union用法

    MYSQL中的UNION UNION在进行表链接后会筛选掉重复的记录,所以在表链接后会对所产生的结果集进行排序运算,删除重复的记录再返回结果. 举例说明: select * from table1 u ...

  4. java使用commons-fileupload进行文件上传

    java中使用文件上传时需要使用特定的类库,这里使用commons-files类库进行文件上传,在http://commons.apache.org/proper/commons-fileupload ...

  5. Java集合篇一:ArrayList

    package com.test.collection; /** * 自定义ArrayList容器 * * 1.实现原理:底层封装数组 * * 2.查询 * LinkList 相较 ArrayList ...

  6. 【代码笔记】Java文件的输入输出(1)——Java.io包的初步理解

    Java里面文件的输入输出全部在java.io包里面. Java.io包里面所有的类都需要掌握. java.io包里面所有的东西都在上面了. 包里面的相关类.异常等树关系如下 类分层结构 java.l ...

  7. Windows内存管理系列

    http://msdn.microsoft.com/en-us/library/hh851882(v=vs.85).aspx Managing Heap Memory Managing Memory- ...

  8. Selenium2学习(十)-- iframe定位

    前言 有很多小伙伴在拿163作为登录案例的时候,发现不管怎么定位都无法定位到,到底是什么鬼呢,本篇详细介绍iframe相关的切换 以http://mail.163.com/登录页面10为案例,详细介绍 ...

  9. zan-framework mysql连接

    ①根据文档内容要配置sqlmap连接池的读写白名单 http://doc.zanphp.io/zh/libs/connection_pool.html 示例代码 // demo.demo.demo_s ...

  10. vue-cli3 项目从搭建优化到docker部署

    项目地址 vue-cli3-project 欢迎 star 原文地址 https://www.ccode.live/lentoo/list/9?from=art 1. 创建一个vue项目 相信大部分人 ...