iOS学习之SKTagView的使用
SKTagView是一款支持自动布局的标签tag.
特性:
-流式展示标签
-可以配置标签的颜色、事件、间隔、外边距等
-支持Auto layout
-可以在UITableViewCell中良好展示
-支持横竖屏切换
-不使用UICollectionView.
// 配置
- (void)configTagView
{
self.label = [[UILabel alloc] initWithFrame:CGRectMake(10, 90, 100, 30)];
self.label.textColor = [UIColor blackColor];
self.label.font = [UIFont systemFontOfSize:13];
self.label.text = @"历史搜索";
[self.view addSubview:self.label];
// 先移除掉所有
[self.tagView removeAllTags];
// 初始化
self.tagView = [[SKTagView alloc] init];
// 整个tagView对应其SuperView的上左下右距离
self.tagView.padding = UIEdgeInsetsMake(10, 10, 10, 10);
// 上下行之间的距离
self.tagView.lineSpacing = 10;
// item之间的距离
self.tagView.interitemSpacing = 20;
// 最大宽度
self.tagView.preferredMaxLayoutWidth = 375;
// @property (assign, nonatomic) CGFloat regularWidth; //!< 固定宽度
// @property (nonatomic,assign ) CGFloat regularHeight; //!< 固定高度
// 原作者没有能加固定宽度的,自己修改源码加上了固定宽度和高度,默认是0,就是标签式布局,如果实现了,那么就是固定宽度高度
// self.tagView.regularWidth = 100;
// self.tagView.regularHeight = 30;
// 开始加载
[self.dataSource enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
// 初始化标签
SKTag *tag = [[SKTag alloc] initWithText:self.dataSource[idx]];
// 标签相对于自己容器的上左下右的距离
tag.padding = UIEdgeInsetsMake(3, 15, 3, 15);
// 弧度
tag.cornerRadius = 3.0f;
// 字体
tag.font = [UIFont boldSystemFontOfSize:12];
// 边框宽度
tag.borderWidth = 0;
// 背景
tag.bgColor = [UIColor colorWithRed:244/255.0 green:244/255.0 blue:244/255.0 alpha:1];
// 边框颜色
tag.borderColor = [UIColor colorWithRed:191/255.0 green:191/255.0 blue:191/255.0 alpha:1];
// 字体颜色
tag.textColor = [UIColor colorWithRed:53/255.0 green:53/255.0 blue:53/255.0 alpha:1];
// 是否可点击
tag.enable = YES;
// 加入到tagView
[self.tagView addTag:tag];
}];
// 点击事件回调
self.tagView.didTapTagAtIndex = ^(NSUInteger idx){
NSLog(@"点击了第%ld个",idx);
};
// 获取刚才加入所有tag之后的内在高度
CGFloat tagHeight = self.tagView.intrinsicContentSize.height;
NSLog(@"高度%lf",tagHeight);
// 根据已经得到的内在高度给SKTagView创建frame
self.tagView.frame = CGRectMake(0, 120, 375, tagHeight);
[self.tagView layoutSubviews];
[self.view addSubview:self.tagView];
}
在UISearchBar的代理方法里面实现搜索的时候隐藏,不搜索的时候显示
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
NSLog(@"%@",searchText);
if (searchText.length == ) {
// 没有文字了
self.label.hidden = NO;
self.tagView.hidden = NO;
}
else
{
self.label.hidden = YES;
self.tagView.hidden = YES;
}
}
下面咱们来看看如何让他在TableViewCell里面实现高度自适应的
- (void)configCell:(MKJTagViewTableViewCell *)cell indexpath:(NSIndexPath *)indexpath
{
[cell.tagView removeAllTags];
cell.tagView.preferredMaxLayoutWidth = [UIScreen mainScreen].bounds.size.width;
cell.tagView.padding = UIEdgeInsetsMake(, , , );
cell.tagView.lineSpacing = ;
cell.tagView.interitemSpacing = ;
cell.tagView.singleLine = NO;
// 给出两个字段,如果给的是0,那么就是变化的,如果给的不是0,那么就是固定的
cell.tagView.regularWidth = ;
cell.tagView.regularHeight = ;
NSArray *arr = [self.dataSource[indexpath.row] valueForKey:@"first"];
[arr enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
SKTag *tag = [[SKTag alloc] initWithText:arr[idx]];
tag.font = [UIFont systemFontOfSize:];
tag.textColor = [UIColor colorWithRed:arc4random() % / 255.0 green:arc4random() % / 255.0 blue:arc4random() % / 255.0 alpha:];
tag.bgColor =[UIColor colorWithRed:arc4random() % / 255.0 green:arc4random() % / 255.0 blue:arc4random() % / 255.0 alpha:];
tag.cornerRadius = ;
tag.enable = YES;
tag.padding = UIEdgeInsetsMake(, , , );
[cell.tagView addTag:tag];
}];
cell.tagView.didTapTagAtIndex = ^(NSUInteger index)
{
NSLog(@"点击了%ld",index);
};
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [tableView fd_heightForCellWithIdentifier:identyfy configuration:^(id cell) {
[self configCell:cell indexpath:indexPath];
}];
}
iOS学习之SKTagView的使用的更多相关文章
- iOS学习-压缩图片(改变图片的宽高)
压缩图片,图片的大小与我们期望的宽高不一致时,我们可以将其处理为我们想要的宽高. 传入想要修改的图片,以及新的尺寸 -(UIImage*)imageWithImage:(UIImage*)image ...
- 【原】iOS学习之事件处理的原理
在iOS学习23之事件处理中,小编详细的介绍了事件处理,在这里小编叙述一下它的相关原理 1.UITouch对象 在触摸事件的处理方法中都会有一个存放着UITouch对象的集合,这个参数有什么用呢? ( ...
- iOS学习笔记——AutoLayout的约束
iOS学习笔记——AutoLayout约束 之前在开发iOS app时一直以为苹果的布局是绝对布局,在IB中拖拉控件运行或者直接使用代码去调整控件都会发上一些不尽人意的结果,后来发现iOS在引入了Au ...
- 【原】iOS学习47之第三方-FMDB
将 CocoaPods 安装后,按照 CocoaPods 的使用说明就可以将 FMDB 第三方集成到工程中,具体请看博客iOS学习46之第三方CocoaPods的安装和使用(通用方法) 1. FMDB ...
- iOS学习路线图
一.iOS学习路线图 二.iOS学习路线图--视频篇 阶 段 学完后目标 知识点 配套学习资源(笔记+源码+PPT) 密码 基础阶段 学习周期:24天 学习后目标: ...
- 黑苹果-IOS学习的开始
深知安装黑苹果的不易,在这里写一下关于我的Thinkpad E430c安装黑苹果教程(Mac版本:Yosemite 10.10.4),希望能够帮助有需要的朋友. 首先贴上我的电脑配置报表: ----- ...
- iOS 学习资源
这份学习资料是为 iOS 初学者所准备的, 旨在帮助 iOS 初学者们快速找到适合自己的学习资料, 节省他们搜索资料的时间, 使他们更好的规划好自己的 iOS 学习路线, 更快的入门, 更准确的定位的 ...
- iOS学习之UINavigationController详解与使用(一)添加UIBarButtonItem
http://blog.csdn.net/totogo2010/article/details/7681879 1.UINavigationController导航控制器如何使用 UINavigati ...
- iOS学习资源个人整理
1208更新: http://www.tuyiyi.com 图翼网 https://github.com/Alamofire/Al ...
随机推荐
- 浅谈python中得import xxx,from xxx import xxx, from xxx import *
在python中import跟from import都是用来导入的,但是导入的机制不同 1.import xxx:导入模块,或者文件夹,对于调用模块或者文件夹中子模块的变量或者函数,需要使用" ...
- java模拟面试 试题
java 四类八种基本数据类型 第一类:整型 byte short int long 第二类:浮点型 float double 第三类:逻辑型 Boolean(取值为 true false) 第四类: ...
- angularjs路由
需要引入angular.js,angular-ui-router.min.js <ul> <li><a href="#home">首页</ ...
- 我需要在Web上完成一个图片上传的功能
我需要在Web上完成一个图片上传的功能. 这个页面需要能从手机中选择图片上传. 首先,这个页面是从微信上面触发的,所以修改了微信的的入口地址,增加了身份识别号作为传参. 跳转到页面的时候,页面先检查身 ...
- Winform 文本框多线程赋值
delegate void SetTextCallback(string text); private void showClientMsg(string text) { // InvokeRequi ...
- 8bit YUV4:2:2格式对应的颜色
红色:YCR YCB YCR YCB....分别为:51e6 515d 51e6 515d 绿色:YCR YCB YCR YCB....分别为:512b 513c 512b 513c 蓝色:YCR Y ...
- applicationContext配置文件中的属性说明
lazy-init:设置只对scop属性为singleton的bean起作用. 1.true:延迟加载:这时在第一次向容器通过getBean索取bean时实例化的. 2.false:表示spring启 ...
- DataTable Linq Example
DataTable CreateTable() { DataTable dtable = new DataTable(); DataColumn dc; //MId CId FId PId dc = ...
- 《Linux内核设计与实现》课本第十八章自学笔记——20135203齐岳
<Linux内核设计与实现>课本第十八章自学笔记 By20135203齐岳 通过打印来调试 printk()是内核提供的格式化打印函数,除了和C库提供的printf()函数功能相同外还有一 ...
- 在代码设置RelativeLayout的属性,比如layout_below
( (RelativeLayout.LayoutParams)holder.ivLvDivider.getLayoutParams()).addRule(RelativeLayout.BELOW, R ...