做项目的时候经常会用到标签,比如说现在很多项目中搜索历史用标签展示 和 选择某个产品的不同属性用标签展示...。网上的有很多封装好的标签,但是作为一个上进的程序员,都希望能有一个自己写的。其实也是一种积累知识的过程。现在的这个标签是根据你的标签的搜字母来排序的。下载地址:https://github.com/liguoliangiOS/LGLTagsView.git先来看下效果:

下面的是代码说明和代码:

(1)包括两个部分:LGLTagsFrame(计算标签的frame)  LGLTagsView(标签的展示的view)

(2)使用注意:请先把标签数组输入LGLTagsFrame计算出标签的总高度 再来利用创建LGLTagsView。

LGLTagsFrame.h

展开

//  Created by 李国良 on 2016/10/15.
// Copyright © 2016年 李国良. All rights reserved.
// #import <Foundation/Foundation.h>
#import <UIKit/UIKit.h> @interface LGLTagsFrame : NSObject /*
* @params tagsArray 标签名称数组
*/
@property (nonatomic, strong) NSArray * tagsArray; /*
* @params tagsTotalHeight 标签的总高度
*/
@property (nonatomic, assign) CGFloat tagsTotalHeight; /*
* @params tagsFrames 每个标签的frame数组
*/
@property (nonatomic, strong) NSMutableArray *tagsFrames; /*
* @params tagsMargin 标签左右之间的间隔 默认是10 (请在给tagsArray赋值之前设置)
*/
@property (nonatomic, assign) CGFloat tagsMargin; /*
* @params tagdPadding 标签上下之间的间隔 默认是10 (请在给tagsArray赋值之前设置)
*/
@property (nonatomic, assign) CGFloat tagsLineMargin; /*
* @params tagdPadding 标签内部的上下左右的间隔 (默认是10) (请在给tagsArray赋值之前设置)
*/
@property (nonatomic, assign) CGFloat tagdPadding; @end

LGLTagsFrame.m

//  Created by 李国良 on 2016/10/15.
// Copyright © 2016年 李国良. All rights reserved.
// #import "LGLTagsFrame.h"
#define WIDTH ([UIScreen mainScreen].bounds.size.width)
#define HEIGHT ([UIScreen mainScreen].bounds.size.height) @interface LGLTagsFrame () @property (nonatomic, strong) UIButton * startButton; @end @implementation LGLTagsFrame - (instancetype)init {
self = [super init];
if (self) {
self.tagsFrames = [NSMutableArray array];
self.tagsMargin = ;
self.tagsLineMargin = ;
self.tagdPadding = ;
}
return self;
} - (void)setTagsArray:(NSArray *)tagsArray {
_tagsArray = tagsArray;
// 去掉重复的title
NSSet * set = [NSSet setWithArray:tagsArray];
NSArray * titleArray = [set allObjects];
NSArray *sortDesc = @[[[NSSortDescriptor alloc] initWithKey:nil ascending:YES]];
NSArray *sort1Array = [titleArray sortedArrayUsingDescriptors:sortDesc];
CGFloat tagsWidth = ;
CGFloat tagY = ;
NSMutableArray * frameA = [NSMutableArray array];
for (NSString * title in sort1Array) { //计算出每个标题的Size
CGSize titleSize = [self sizeWithText:title font:[UIFont systemFontOfSize:] maxW:];
[frameA addObject:NSStringFromCGSize(titleSize)];
}
for (NSInteger i = ; i < frameA.count; i ++) {
CGSize size = CGSizeFromString(frameA[i]);
CGFloat width = size.width + self.tagdPadding;
CGFloat height = size.height + self.tagdPadding;
if ((WIDTH - tagsWidth - self.tagsMargin) < (width)) {
tagY = tagY + height + self.tagsLineMargin;
tagsWidth = ;
}
UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(self.tagsMargin + tagsWidth, tagY, width, height);
[btn setTitle:sort1Array[i] forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
btn.titleLabel.font = [UIFont systemFontOfSize:];
btn.layer.masksToBounds = YES;
btn.layer.cornerRadius = ;
btn.layer.borderWidth = ;
btn.layer.borderColor = [UIColor blackColor].CGColor;
[self.tagsFrames addObject:btn];
tagsWidth = CGRectGetMaxX(btn.frame);
if (i == frameA.count - ) {
self.tagsTotalHeight = CGRectGetMaxY(btn.frame) + self.tagsLineMargin;
}
}
} //计算文字的大小 maxW限制最大宽度 maxW 传MAXFLOAT,没有限制最大的宽度
- (CGSize)sizeWithText:(NSString *)text font:(UIFont *)font maxW:(CGFloat)maxW
{
NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
attrs[NSFontAttributeName] = font;
CGSize maxSize = CGSizeMake(maxW, MAXFLOAT); return [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
} - (void)setTagsMargin:(CGFloat)tagsMargin {
_tagsMargin = tagsMargin;
} - (void)setTagsLineMargin:(CGFloat)tagsLineMargin {
_tagsLineMargin = tagsLineMargin;
} - (void)setTagdPadding:(CGFloat)tagdPadding {
_tagdPadding = tagdPadding;
} @end

展开

LGLTagsView.h

//  Created by 李国良 on 2016/10/17.
// Copyright © 2016年 李国良. All rights reserved.
// #import <UIKit/UIKit.h> typedef void(^TagSelectBlock)(NSString * tagName); @interface LGLTagsView : UIView /**
* @params frame 高度请传 LGLTagsFrame的 tagsTotalHeight
* @params tagsFrame 请传 LGLTagsFrame 的 tagsFrames
*/
- (instancetype)initWithFrame:(CGRect)frame tagsFrame:(NSMutableArray *)tagsFrame selectTagBlock:(TagSelectBlock)block; /*
* @params isSelected 是是否要有选中的效果 默认有选中的效果
*/
@property (nonatomic, assign) BOOL isSelected; /*
* @params tagsSelectedColor 是修改选中tag的背景色颜色(默认 orange) 在没有选中效果的时候设置无效
*/
@property (nonatomic, strong) UIColor * tagsSelectedColor; @end

展开

LGLTagsView.m

//  Created by 李国良 on 2016/10/17.
// Copyright © 2016年 李国良. All rights reserved.
// #import "LGLTagsView.h"
@interface LGLTagsView () @property (nonatomic, strong) NSMutableArray * tagsFrame;
@property (nonatomic, strong) UIButton * startButton;
@property (nonatomic, copy) TagSelectBlock block; @end @implementation LGLTagsView - (instancetype)initWithFrame:(CGRect)frame tagsFrame:(NSMutableArray *)tagsFrame selectTagBlock:(TagSelectBlock)block {
self = [super initWithFrame:frame];
if (self) {
self.tagsFrame = tagsFrame;
self.isSelected = YES;
self.tagsSelectedColor = [UIColor orangeColor];
self.block = block;
[self createTagsView];
}
return self;
} - (void)createTagsView {
for (UIButton * tags in self.tagsFrame) {
[tags addTarget:self action:@selector(tagsClink:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:tags];
}
} - (void)tagsClink:(UIButton *)button {
if (self.isSelected) {
if(button !=self.startButton){
self.startButton.selected = NO;
[self.startButton setBackgroundColor:[UIColor whiteColor]];
self.startButton.layer.borderColor = [UIColor blackColor].CGColor;
self.startButton = button;
}
self.startButton.selected=YES;
if (self.startButton.selected) {
[self.startButton setBackgroundColor:self.tagsSelectedColor];
self.startButton.layer.borderColor = [UIColor whiteColor].CGColor;
}
}
self.block(button.titleLabel.text);
} - (void)setIsSelected:(BOOL)isSelected {
_isSelected = isSelected;
} - (void)setTagsSelectedColor:(UIColor *)tagsSelectedColor {
_tagsSelectedColor = tagsSelectedColor;
} - (NSMutableArray *)tagsFrame {
if (!_tagsFrame ) {
_tagsFrame = [NSMutableArray array];
}
return _tagsFrame;
}
@end

展开

文件到此结束

下面说明一下具体使用的方法:

 LGLTagsFrame * frame = [[LGLTagsFrame alloc] init];
frame.tagsArray = [dataSource copy];
LGLTagsView * tagView = [[LGLTagsView alloc] initWithFrame:CGRectMake(, , WIDTH, frame.tagsTotalHeight) tagsFrame:frame.tagsFrames selectTagBlock:^(NSString *tagName) {
// 在这里获得标签的点击回调的值
}
}];
[self.view addSubview:tagView];

有修改意见的欢迎来骚扰!

LGLTagsView的更多相关文章

随机推荐

  1. Android笔记——Android五大布局

    一.五大布局 Android的界面是有布局和组件协同完成的,布局好比是建筑里的框架,而组件则相当于建筑里的砖瓦.组件按照布局的要求依次排列,就组成了用户所看见的界面.Android的五大布局分别是Li ...

  2. AntV 数据可视化解决方案发布

    今天蚂蚁金服发布了一套数据可视化规范AntV. AntV 是一套专业的数据可视化规范,这套规范的目的是为了让可视化的使用者更懂数据可视化.这套规范是蚂蚁金服在可视化建设过程中的理论沉淀,它可以很好得指 ...

  3. android 股票数据通过日K获取周K的数据 算法 源码

    目前的数据是从新浪接口获取的, http://biz.finance.sina.com.cn/stock/flash_hq/kline_data.php?symbol=sh600000&end ...

  4. KnockoutJS 3.X API 第四章 数据绑定(5) 控制流component绑定

    本节目录: 一个例子 API 备注1:仅模板式的component 备注2:component虚拟绑定 备注3:传递标记到component绑定 内存管理 一个例子 First instance, w ...

  5. 35.按要求编写Java程序: (1)编写一个接口:InterfaceA,只含有一个方法int method(int n); (2)编写一个类:ClassA来实现接口InterfaceA,实现int method(int n)接口方 法时,要求计算1到n的和; (3)编写另一个类:ClassB来实现接口InterfaceA,实现int method(int n)接口 方法时,要求计算n的阶乘(n

      35.按要求编写Java程序: (1)编写一个接口:InterfaceA,只含有一个方法int method(int n): (2)编写一个类:ClassA来实现接口InterfaceA,实现in ...

  6. 注意HTML的语言编码charset

    注意HTML的语言编码的重要性 目录 charset编码重要性 charset在html什么地方 charset标签 编码种类 charset utf-8介绍 charset GB2312介绍 推荐网 ...

  7. js获取url地址中的参数

    <script type="text/javascript"> function GetQueryString(name) { var reg = new RegExp ...

  8. Java多线程系列--“JUC线程池”01之 线程池架构

    概要 前面分别介绍了"Java多线程基础"."JUC原子类"和"JUC锁".本章介绍JUC的最后一部分的内容——线程池.内容包括:线程池架构 ...

  9. ViewPager的缓存机制

    1.实现Viewpager的页面懒加载: 在某些情况下,例如使用ViewPager查看多张大图,此时多张图片不能一次性载入,只有在浏览该页面时才载入(或者预先载入下一页面)页面的具体内容. 2.可控V ...

  10. html5的感想

    作为一名前端攻城尸,每天必不可少的就是要学习新的知识,直到you get it. 今天,又一次学习了html5,每一次学习都会有新的感受. 1.记得第一次学习的时候只是觉得html5多了一些新的标签, ...