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的使用的更多相关文章

  1. iOS学习-压缩图片(改变图片的宽高)

    压缩图片,图片的大小与我们期望的宽高不一致时,我们可以将其处理为我们想要的宽高. 传入想要修改的图片,以及新的尺寸 -(UIImage*)imageWithImage:(UIImage*)image ...

  2. 【原】iOS学习之事件处理的原理

    在iOS学习23之事件处理中,小编详细的介绍了事件处理,在这里小编叙述一下它的相关原理 1.UITouch对象 在触摸事件的处理方法中都会有一个存放着UITouch对象的集合,这个参数有什么用呢? ( ...

  3. iOS学习笔记——AutoLayout的约束

    iOS学习笔记——AutoLayout约束 之前在开发iOS app时一直以为苹果的布局是绝对布局,在IB中拖拉控件运行或者直接使用代码去调整控件都会发上一些不尽人意的结果,后来发现iOS在引入了Au ...

  4. 【原】iOS学习47之第三方-FMDB

    将 CocoaPods 安装后,按照 CocoaPods 的使用说明就可以将 FMDB 第三方集成到工程中,具体请看博客iOS学习46之第三方CocoaPods的安装和使用(通用方法) 1. FMDB ...

  5. iOS学习路线图

    一.iOS学习路线图   二.iOS学习路线图--视频篇       阶 段 学完后目标 知识点 配套学习资源(笔记+源码+PPT) 密码 基础阶段 学习周期:24天       学习后目标:    ...

  6. 黑苹果-IOS学习的开始

    深知安装黑苹果的不易,在这里写一下关于我的Thinkpad E430c安装黑苹果教程(Mac版本:Yosemite 10.10.4),希望能够帮助有需要的朋友. 首先贴上我的电脑配置报表: ----- ...

  7. iOS 学习资源

    这份学习资料是为 iOS 初学者所准备的, 旨在帮助 iOS 初学者们快速找到适合自己的学习资料, 节省他们搜索资料的时间, 使他们更好的规划好自己的 iOS 学习路线, 更快的入门, 更准确的定位的 ...

  8. iOS学习之UINavigationController详解与使用(一)添加UIBarButtonItem

    http://blog.csdn.net/totogo2010/article/details/7681879 1.UINavigationController导航控制器如何使用 UINavigati ...

  9. iOS学习资源个人整理

    1208更新: http://www.tuyiyi.com                                    图翼网 https://github.com/Alamofire/Al ...

随机推荐

  1. 即时聊天IM之二 openfire 整合现有系统用户

    合肥程序员群:49313181.    合肥实名程序员群:128131462 (不愿透露姓名和信息者勿加入) Q  Q:408365330     E-Mail:egojit@qq.com  综述: ...

  2. WinDbg 蓝屏dump分析教程

    一.WinDbg是什么?它能做什么? WinDbg是在windows平台下,强大的用户态和内核态调试工具.它能够通过dmp文件轻松的定位到问题根源,可用于分析蓝屏.程序崩溃(IE崩溃)原因,是我们日常 ...

  3. 《精通C#》第十七章

    进程简单的说就是一个正在运行的程序,包含了运行该程序所需要的一切资源以及内存分配.线程是进程的基本执行单元,在进程的入口点(类似main())创建的第一个线程称之为主线程.只有一个主线程的进程是线程安 ...

  4. rem

    "em"是相对于其父元素来设置字体大小的,这样就会存在一个问题,进行任何元素设置,都有可能需要知道他父元素的大小,在我们多次使用时,就会带来无法预知的错误风险.而rem是相对于根元 ...

  5. UEditor-从客户端(editorValue="<p>asd</p>")中检测到有潜在危险的 Request.Form 值。

    在用富文本编辑器时经常会遇到的问题是asp.net报的”检测到有潜在危险的 Request.Form 值“一般的解法是在aspx页面   page  标签中加上 validaterequest='fa ...

  6. Getting Started with JD Edwards EnterpriseOne Interoperability

      Overview Every enterprise holds a forest of branched system spread across a number of business uni ...

  7. kettle 使用JAVA代码进行执行

    kettle 设计完成之后,可以在设计工具中进行调用,也可以使用java代码进行调用.   1.通过文件方式执行转换.   public static void runTransfer(String[ ...

  8. CentOS 7 Mysql yum源

    CentOS 7 安装Mysqlrpm -ivh http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpmyum install m ...

  9. [暴力搜索] POJ 3087 Shuffle'm Up

    Shuffle'm Up Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10003   Accepted: 4631 Des ...

  10. git branch(git 的branch 使用方法)

    查看分支:         $ git branch    该命令会类出当先项目中的所有分支信息,其中以*开头的表示当前所在的分支.参数-r列出远程仓库中的分支,而-a则远程与本地仓库的全部分支. 创 ...