前一阵时间,看过这样一个demo,代码不多,但是简洁易懂。

转自:

//  代码地址: https://github.com/iphone5solo/PYSearch

//  代码地址: http://www.code4app.com/thread-11175-1-1.html

/**  添加和布局标签 */
- (NSArray *)addAndLayoutTagsWithTagsContentView:(UIView *)contentView tagTexts:(NSArray<NSString *> *)tagTexts;
{
// 清空标签容器的子控件
[contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
// 添加热门搜索标签
NSMutableArray *tagsM = [NSMutableArray array];
for (int i = 0; i < tagTexts.count; i++) {
UILabel *label = [self labelWithTitle:tagTexts[i]];
[label addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tagDidCLick:)]];
[contentView addSubview:label];
[tagsM addObject:label];
} // 计算位置
CGFloat currentX = 0;
CGFloat currentY = 0;
CGFloat countRow = 0;
CGFloat countCol = 0; // 调整布局
for (UILabel *subView in tagsM) {
// 当搜索字数过多,宽度为contentView的宽度
if (subView.py_width > contentView.py_width) subView.py_width = contentView.py_width;
if (currentX + subView.py_width + PYMargin * countRow > contentView.py_width) { // 得换行
subView.py_x = 0;
subView.py_y = (currentY += subView.py_height) + PYMargin * ++countCol;
currentX = subView.py_width;
countRow = 1;
} else { // 不换行
subView.py_x = (currentX += subView.py_width) - subView.py_width + PYMargin * countRow;
subView.py_y = currentY + PYMargin * countCol;
countRow ++;
}
}
// 设置contentView高度
contentView.py_height = CGRectGetMaxY(contentView.subviews.lastObject.frame);
// 设置头部高度
self.baseSearchTableView.tableHeaderView.py_height = self.headerContentView.py_height = CGRectGetMaxY(contentView.frame) + PYMargin * 2;
// 重新赋值, 注意:当操作系统为iOS 9.x系列的tableHeaderView高度设置失效,需要重新设置tableHeaderView
[self.baseSearchTableView setTableHeaderView:self.baseSearchTableView.tableHeaderView];
return [tagsM copy];
}

/** 添加标签 */

- (UILabel *)labelWithTitle:(NSString *)title

{

UILabel *label = [[UILabel alloc] init];

label.userInteractionEnabled = YES;

label.font = [UIFont systemFontOfSize:12];

label.text = title;

label.textColor = [UIColor grayColor];

label.backgroundColor = [UIColor py_colorWithHexString:@"#fafafa"];

label.layer.cornerRadius = 3;

label.clipsToBounds = YES;

label.textAlignment = NSTextAlignmentCenter;

[label sizeToFit];

label.py_width += 20;

label.py_height += 14;

return label;

}

【转】iOS,搜索标签布局的更多相关文章

  1. TODO:Laravel 使用blade标签布局页面

    TODO:Laravel 使用blade标签布局页面 本文主要介绍Laravel的标签使用,统一布局页面.主要用到到标签有@yield,@ stack,@extends,@section,@stop, ...

  2. 《IT蓝豹》高仿花田ios版标签移动效果

    高仿花田ios版标签移动效果,长按每一个item拖动到自己想要位置后,后面位置移动补全效果 . 本项目适合研究gridview拖拽效果的朋友下载. 学习android动画特效. 本项目主要靠DragG ...

  3. 制作chm无搜索标签解决方法

    chm无搜索标签解决方法: 1.hpp文件中设置Full-text search=Yes 2.下面[windows]有个数字设置为0x420, 网上普通流传的是0x20 === chm制作一般要三个文 ...

  4. {...formItemLayout} 标签布局

    {...formItemLayout}是reactjs中属性的写法{...props},formItemLayout标签布局,wrapperCol需要为输入控件设置布局样式时,和label 标签布局, ...

  5. Android商城开发系列(五)—— 商城首页回到顶部和搜索框布局实现

    今天我们来开发商城的首页[输入搜索框]布局和点击右下角图片回到顶部的效果 搜索功能在App中很常见,尤其是在商城类的项目当中,一般都会提供很强大的搜索功能,App的搜索布局一般都是在App的顶部,如下 ...

  6. iOS内存管理布局及管理方案-理论篇

    苹果设备备受欢迎的背后离不开iOS优秀的内存管理机制,那iOS的内存布局及管理方案是怎样的呢?我们一起研究下. 内存管理分为五大块 栈区(stack):线性结构,内存连续,系统自己管理内存,程序运行记 ...

  7. ios UI 适配布局相关文章

    1. http://lvwenhan.com/ios/430.html 2 . http://blog.csdn.net/liangliang103377/article/details/400822 ...

  8. HTML <!DOCTYPE> 标签 布局引用的几种方法 行级元素与块级元素

    HTML <!DOCTYPE> 标签 <!DOCTYPE html> <html> <head> <title>文档的标题</titl ...

  9. iOS 全屏布局

    edgesForExtendedLayout属性用于替代wantsFullScreenLayout,控制页面显示的范围,默认值是UIRectEdgeAll automaticallyAdjustsSc ...

随机推荐

  1. Bonobo创建新库出错,解决方案

    创建新库出错如下: Native library pre-loader is trying to load native SQLite library "D:\wwwroot\localho ...

  2. wnmp环境搭建

    windows下配置nginx+php环境 刚看到nginx这个词,我很好奇它的读法(engine x),我的直译是“引擎x”,一般引“擎代”表了性能,而“x”大多出现是表示“xtras(额外的效果) ...

  3. (转)解决 ORA-12514: TNS: 监听程序当前无法识别连接描述符中请求的服务

    下面操作默认在安装Oralce数据库的服务器上运行. 1)确保Oracle 基本服务都已启动 OracleDBConsoleorcl OracleOraDb11g_home1TNSListener O ...

  4. javaScript的内置对象

    javaScript 有11种内置对象: Array . String .Date .Math . Boolean .Number . Function .Global .Error . RegExp ...

  5. ES6之数组扩展方法【一】(相当好用)

    form 转化为真正的数组 先说一下使用场景,在Js中,我们要经常操作DOM,比如获取全部页面的input标签,并且找到类型为button的元素,然后给这个按钮注册一个点击事件,我们可能会这样操作: ...

  6. Web Mercator Non-Conformal, Non-Mercator

    public static void XYtoGL(Coordinate coordinate) { double R = 6378137; coordinate.x = coordinate.x / ...

  7. ZeroC Ice启用SSL通讯的配置

    Zeroc ICE ( Internet Communications Engine )中间件号称标准统一,开源,跨平台,跨语言,分布式,安全,服务透明,负载均衡,面向对象,性能优越,防火墙穿透,通讯 ...

  8. 网络安全——数据的加密与签名,RSA介绍

    一. 密码概述 发送者对明文进行加密然后生成密文,接受者再对密文解密得到明文的过程. 现在使用的所有加密算法都是公开的!但是密钥肯定不是公开的. 1 散列(哈希)函数 通常有MD5.SHA1.SHA2 ...

  9. iOS 学习 - 3.仿qq列表

    完整代码放在 github 上面 https://github.com/slodier/SimilarQQ

  10. mysq l错误Table ‘./mysql/proc’ is marked as crashed and should be repaired

    续上一篇,解决了上一篇中的问题后,启动成功,但是在数据库中操作会存在一些问题,一些操作报一下异常: Table './mysql/proc' is marked as crashed and shou ...