Demo效果1:
![](https://i-blog.csdnimg.cn/blog_migrate/752d357969e6960ac67ad167191594ec.webp?x-image-process=image/format,png)
效果1.gif
Demo效果2:
![](https://i-blog.csdnimg.cn/blog_migrate/ae46bc5b1c8fb0e55c02c7d589cf2cd5.webp?x-image-process=image/format,png)
效果2.gif
Demo效果3:
![](https://i-blog.csdnimg.cn/blog_migrate/b2a6b5940f2b74f70aaab382d1dd0eb1.webp?x-image-process=image/format,png)
效果3.gif
Demo演示:
1.使用cocoapods引入YZTagListView,或者直接拖入YZTagListView文件夹到项目中
2.导入YZTagListView.h头文件
#import"YZTagList.h"
3.创建YZTagListView控件
YZTagList *tagList = [[YZTagList alloc] init];tagList.backgroundColor= [UIColorbrownColor];
_tagList = tagList;
4.设置YZTagListView属性(可选)
// 高度可以设置为0,会自动跟随标题计算
tagList.frame = CGRectMake(0,64, self.view.bounds.size.width,0);
// 设置标签背景色
tagList.tagBackgroundColor = [UIColorcolorWithRed:20/ 255.0 green:160 /255.0blue:250/255.0alpha:1];
// 设置标签颜色
tagList.tagColor = [UIColor whiteColor];
// 设置标签删除图片
tagList.tagDeleteimage = [UIImageimageNamed:@"chose_tag_close_icon"];
5.添加标签
/**
* 添加标签
*
* @param tagStr 标签文字
*/-(void)addTag:(NSString *)tagStr;
6.添加多个标签
/** * 添加多个标签 ** @param tagStrs 标签数组,数组存放(NSString *) */- (void)addTags:(NSArray *)tagStrs;
7.删除标签
/**
* 删除标签
*
* @param tagStr 标签文字
*/-(void)deleteTag:(NSString *)tagStr;
8.监听标签点击
/**
* 点击标签,执行Block
*/@property(nonatomic,strong)void(^clickTagBlock)(NSString*tag);列如:点击标签,删除标签// 点击标签,就会调用__weaktypeof(_tagList) weakTagList = _tagList; _tagList.clickTagBlock = ^(NSString*tag){ [weakTagList deleteTag:tag]; };
9.排序功能
属性:/**
* 是否需要排序功能
*/@property(nonatomic,assign)BOOLisSort;/**
* 在排序的时候,放大标签的比例,必须大于1
*/@property(nonatomic,assign)CGFloatscaleTagInSort;
实例:
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view.
NSArray*tags = @[@"小码哥",@"小码哥1",@"小码哥2",@"小码哥3",@"iOS学院",@"iOS学院1",@"iOS学院2",@"iOS学院3",@"吖了个峥",@"吖了个峥1",@"吖了个峥2",@"吖了个峥3"];
// 创建标签列表
YZTagList *tagList = [[YZTagList alloc] init];
// 高度可以设置为0,会自动跟随标题计算
tagList.frame =CGRectMake(0,64,self.view.bounds.size.width,self.view.bounds.size.height -64);
// 设置排序时,缩放比例
tagList.scaleTagInSort =1.3;
// 需要排序
tagList.isSort =YES;
// 标签尺寸
tagList.tagSize =CGSizeMake(80,30);
// 不需要自适应标签列表高度
tagList.isFitTagListH =NO;
[self.view addSubview:tagList];
// 设置标签背景色
tagList.tagBackgroundColor = [UIColorcolorWithRed:20/255.0green:160/255.0blue:250/255.0alpha:1];
// 设置标签颜色
tagList.tagColor = [UIColorwhiteColor];/**
* 这里一定先设置标签列表属性,然后最后去添加标签
*/[tagList addTags:tags];}
更多用法,请参考Demo
源码
点击这下载源代码