惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Secure Thoughts
雷峰网
雷峰网
罗磊的独立博客
T
The Blog of Author Tim Ferriss
阮一峰的网络日志
阮一峰的网络日志
量子位
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
云风的 BLOG
云风的 BLOG
人人都是产品经理
人人都是产品经理
GbyAI
GbyAI
Cisco Talos Blog
Cisco Talos Blog
Engineering at Meta
Engineering at Meta
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
A
About on SuperTechFans
D
Darknet – Hacking Tools, Hacker News & Cyber Security
The Cloudflare Blog
Know Your Adversary
Know Your Adversary
T
Threat Research - Cisco Blogs
Spread Privacy
Spread Privacy
D
DataBreaches.Net
T
The Exploit Database - CXSecurity.com
K
Kaspersky official blog
Cyberwarzone
Cyberwarzone
爱范儿
爱范儿
U
Unit 42
Security Latest
Security Latest
M
MIT News - Artificial intelligence
月光博客
月光博客
Scott Helme
Scott Helme
G
Google Developers Blog
有赞技术团队
有赞技术团队
T
Tor Project blog
宝玉的分享
宝玉的分享
Y
Y Combinator Blog
博客园 - Franky
H
Hackread – Cybersecurity News, Data Breaches, AI and More
aimingoo的专栏
aimingoo的专栏
The GitHub Blog
The GitHub Blog
V
V2EX
B
Blog
Apple Machine Learning Research
Apple Machine Learning Research
S
Securelist
博客园 - 三生石上(FineUI控件)
Blog — PlanetScale
Blog — PlanetScale
TaoSecurity Blog
TaoSecurity Blog
Stack Overflow Blog
Stack Overflow Blog
P
Proofpoint News Feed
腾讯CDC
D
Docker
Google Online Security Blog
Google Online Security Blog

土土哥的Blog

好久没有更新博客=。= 阿里巴巴国际无线技术部 - 招人啦~求iOS、Android、Java 转-阿里巴巴国际无线技术部 - 在这里遇见最好的自己 反编译分析并模拟实现methodSignatureForSelector方法 反编译分析Xcode8的Bug, release下连续两次调用有二级指针参数的空方法会Crash 有趣的Autolayout示例5-Masonry实现 在对象dealloc的后期执行Task-开源库TTGDeallocTaskHelper 用QuartzCode快速实现一个收藏动画 开源项目-拼图验证控件TTGPuzzleVerify的实现 Swift开源Mac App - BingWallPaper 有趣的Autolayout示例4-Masonry实现 翻译-为什么objc_msgSend必须用汇编实现 API返回结果设计经验与总结 结合访问Out Parameters出现EXC_BAD_ACCESS的例子,反编译汇编解读__autoreleasing 总结一些iOS项目中组织代码的方法 对组件化与模块化的思考与总结 有趣的Autolayout示例3-Masonry实现 Swift开源项目: TTGEmojiRate的实现 Swift写的库-TTGSnackbar 有趣的Autolayout示例2-Masonry实现 解决iOS项目的版本兼容问题-结合宏、Category和Runtime 用Runtime的手段填充任意NSObject对象的nil属性 有趣的Autolayout示例-Masonry实现 UITextView编辑时插入自定义表情-续-自定义表情图片的大小 RPC框架Thrift例子-PHP调用C++后端程序 GCD使用经验与技巧浅谈 为GCD队列绑定NSObject类型上下文数据-利用__bridge_retained(transfer)转移内存管理权 Enum-枚举的正确使用-Effective-Objective-C-读书笔记-Item-5 @autoreleasepool-内存的分配与释放 有关宏定义的经验与技巧-简化代码-增强Log Effective-Objective-C-读书笔记-Item-4-如何正确定义常量 UITextView编辑时插入自定义表情-简单的图文混编 关于评论不见了=。= Entity和Model的不同-关于代码的数据层 一次审核被拒的经历-关于iCloud到底应该备份什么数据 Block类型变量-缓存Http请求与回调 提升UITableView性能-复杂页面的优化 NSString的Copy与内存分配 利用NSProxy实现消息转发-模块化的网络接口层设计-原创 Effective-Objective-C-读书笔记-Item-3 Effective-Objective-C-读书笔记-Item-2 Effective-Objective-C-读书笔记-Item-1 iOS项目的目录结构-原创 Android开源库-LinkTextView-原创 第一篇Blog
开源项目-TTGTagCollectionView
土土哥 · 2015-12-31 · via 土土哥的Blog

更新

  • 2017-02-27: 增加两种Alignment布局类型;增加点击前的回调判断;增加选中Tag上限
  • 2016-12-25: 不再依赖UICollectionView,减少了位置刷新次数,提高性能;增加了alignment属性,可以靠左中右排列
  • 2016-10-29: 增加Tag的阴影和ContentInset的设置,更新了Demo
  • 2016-10-17: 增加了水平滑动;Autolayout适配;UITableViewCell例子等
  • 2016-02-18: 修复了超长标签导致排版错误的Bug

前言

这段时间做项目的时候,总是需要显示一些“标签”样式的内容,但是又找不到用的顺手的库,所以琢磨了几天,自己实现了出来,就有了这个库:TTGTagCollectionView。如果只需要显示文字标签的话,直接使用TTGTextTagCollectionView,需要自己定义标签的话,就用TTGTagCollectionView,效果如下:

ScreenShot

支持五种布局排版:

Alignment

CocoaPods: pod "TTGTagCollectionView"
Carthage: github "zekunyan/TTGTagCollectionView"

Github地址: https://github.com/zekunyan/TTGTagCollectionView

只显示文字标签 - TTGTextTagCollectionView

基本使用

只显示文字标签的话,直接用TTGTextTagCollectionView类就可以了:

1
2
3
TTGTextTagCollectionView *tagCollectionView = [[TTGTextTagCollectionView alloc] initWithFrame:CGRectMake(20, 20, 200, 200)];
[self.view addSubview:tagCollectionView];
[tagCollectionView addTags:@[@"TTG", @"Tag", @"collection", @"view"]];

接收点击事件 - 实现Delegate

如果想在标签被点击时得到通知,实现对应的Protocol即可,定义如下:

1
2
3
4
5
6
7
8
9
10
11
@protocol TTGTextTagCollectionViewDelegate <NSObject>
@optional

- (BOOL)textTagCollectionView:(TTGTextTagCollectionView *)textTagCollectionView canTapTag:(NSString *)tagText atIndex:(NSUInteger)index currentSelected:(BOOL)currentSelected;


- (void)textTagCollectionView:(TTGTextTagCollectionView *)textTagCollectionView didTapTag:(NSString *)tagText atIndex:(NSUInteger)index selected:(BOOL)selected;


- (void)textTagCollectionView:(TTGTextTagCollectionView *)textTagCollectionView updateContentSize:(CGSize)contentSize;
@end

自定义文字标签样式

如果想对标签的样式做定制,可以设置以下属性:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59

@property (assign, nonatomic) BOOL enableTagSelection;


@property (strong, nonatomic) UIFont *tagTextFont;


@property (strong, nonatomic) UIColor *tagTextColor;
@property (strong, nonatomic) UIColor *tagSelectedTextColor;


@property (strong, nonatomic) UIColor *tagBackgroundColor;
@property (strong, nonatomic) UIColor *tagSelectedBackgroundColor;


@property (assign, nonatomic) CGFloat tagCornerRadius;
@property (assign, nonatomic) CGFloat tagSelectedCornerRadius;


@property (assign, nonatomic) CGFloat tagBorderWidth;
@property (assign, nonatomic) CGFloat tagSelectedBorderWidth;
@property (strong, nonatomic) UIColor *tagBorderColor;
@property (strong, nonatomic) UIColor *tagSelectedBorderColor;


@property (nonatomic, copy) UIColor *tagShadowColor;
@property (nonatomic, assign) CGSize tagShadowOffset;
@property (nonatomic, assign) CGFloat tagShadowRadius;
@property (nonatomic, assign) CGFloat tagShadowOpacity;


@property (nonatomic, assign) TTGTagCollectionScrollDirection scrollDirection;


@property (nonatomic, assign) TTGTagCollectionAlignment alignment;


@property (nonatomic, assign) NSUInteger numberOfLines;


@property (nonatomic, assign) NSUInteger selectionLimit;


@property (assign, nonatomic) CGSize extraSpace;


@property (assign, nonatomic) CGFloat horizontalSpacing;

@property (assign, nonatomic) CGFloat verticalSpacing;


@property (nonatomic, assign) UIEdgeInsets contentInset;


@property (nonatomic, assign, readonly) CGSize contentSize;


@property (nonatomic, assign) BOOL showsHorizontalScrollIndicator;
@property (nonatomic, assign) BOOL showsVerticalScrollIndicator;

增加、删除标签

1
2
3
4
5
6
7
8
9
10
11

- (void)addTag:(NSString *)tag;

- (void)addTags:(NSArray <NSString *> *)tags;


- (void)removeTag:(NSString *)tag;

- (void)removeTagAtIndex:(NSUInteger)index;

- (void)removeAllTags;

在代码里控制标签的选中状态

1
- (void)setTagAtIndex:(NSUInteger)index selected:(BOOL)selected;

获取所有、选中、未选中标签

1
2
3
4
5
- (NSArray <NSString *> *)allTags

- (NSArray <NSString *> *)allSelectedTags

- (NSArray <NSString *> *)allNotSelectedTags

重新加载 - Reload

- (void)reload方法重新加载所有标签。

显示自定义的标签控件View - TTGTagCollectionView

如果想自己定义标签View,如同时显示图像、文字、按钮,可以用TTGTagCollectionView类实现。

基本要求 - 实现DataSource和Delegate

DataSource的定义如下:

1
2
3
4
5
6
7
8
@protocol TTGTagCollectionViewDataSource <NSObject>
@required

- (NSUInteger)numberOfTagsInTagCollectionView:(TTGTagCollectionView *)tagCollectionView;


- (UIView *)tagCollectionView:(TTGTagCollectionView *)tagCollectionView tagViewForIndex:(NSUInteger)index;
@end

Delegate的定义如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@protocol TTGTagCollectionViewDelegate <NSObject>
@required

- (CGSize)tagCollectionView:(TTGTagCollectionView *)tagCollectionView sizeForTagAtIndex:(NSUInteger)index;

@optional

- (BOOL)tagCollectionView:(TTGTagCollectionView *)tagCollectionView shouldSelectTag:(UIView *)tagView atIndex:(NSUInteger)index;


- (void)tagCollectionView:(TTGTagCollectionView *)tagCollectionView didSelectTag:(UIView *)tagView atIndex:(NSUInteger)index;


- (void)tagCollectionView:(TTGTagCollectionView *)tagCollectionView updateContentSize:(CGSize)contentSize;
@end

跟UITableView的思路一致~

设置标签的行距、间隔

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

@property (nonatomic, assign) TTGTagCollectionScrollDirection scrollDirection;


@property (nonatomic, assign) TTGTagCollectionAlignment alignment;


@property (nonatomic, assign) NSUInteger numberOfLines;


@property (assign, nonatomic) CGFloat horizontalSpacing;

@property (assign, nonatomic) CGFloat verticalSpacing;


@property (nonatomic, assign) UIEdgeInsets contentInset;


@property (nonatomic, assign, readonly) CGSize contentSize;


@property (nonatomic, assign) BOOL showsHorizontalScrollIndicator;
@property (nonatomic, assign) BOOL showsVerticalScrollIndicator;

重新加载 - Reload

- (void)reload方法重新加载所有标签。

End

2015最后一天~
新年快乐~