LGLTagsView
做项目的时候经常会用到标签,比如说现在很多项目中搜索历史用标签展示 和 选择某个产品的不同属性用标签展示...。网上的有很多封装好的标签,但是作为一个上进的程序员,都希望能有一个自己写的。其实也是一种积累知识的过程。现在的这个标签是根据你的标签的搜字母来排序的。下载地址: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的更多相关文章
随机推荐
- Atitit Atitit 图像处理之 Oilpaint油画滤镜 水彩画 源码实现
Atitit Atitit 图像处理之 Oilpaint油画滤镜 水彩画 源码实现 1.1. 具体原理参考1 2. 水彩画滤镜算法如下:1 2.1. 这个其实就是灰度层次降低维度的过程.2 2.2. ...
- Atitit机器学习原理与概论book attilax总结
Atitit机器学习原理与概论book attilax总结 <机器学习(决战大数据时代!IT技术人员不得不读!)>((美)米歇尔(Mitchell)[简介_书评_在线阅读] -1 < ...
- DNS正向解析与反向解析
DNS:(Domain Name System,域名系统),因特网上作为域名和IP地址相互映射的一个分布式数据库,能够使用户更方便的访问互联网, 而不去记住能够被机器直接读取的IP数串.通过主机名,最 ...
- PHP实现RESTful风格的API实例(一)
最近看了一些关于RESTful的资料,自己动手也写了一个RESTful实例,以下是源码 目录详情: restful/ Request.php 数据操作类 Response.php 输出类 index. ...
- IOS开发之控件篇UICollectionViewControllor第一章 - 普通介绍
1.介绍 UICollectionView和UICollectionViewControllor是IOS6.0后引入的新控件 使用UICollectionView必须实现三个接口: UICollect ...
- socket编程的select模型
在掌握了socket相关的一些函数后,套接字编程还是比较简单的,日常工作中碰到很多的问题就是客户端/服务器模型中,如何让服务端在同一时间高效的处理多个客户端的连接,我们的处理办法可能会是在服务端不停的 ...
- Android进程间通信之socket通信
用Java中的socket编程. 通过socket实现两个应用之间的通信,可以接收和发送数据,同时将接收到的数据显示在activity界面上. Server端: ServerLastly.java p ...
- ajax检查用户名是否存在
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.c ...
- C#中string.format的格式和用法
String.Format 方法的几种定义: String.Format (String, Object) 将指定的 String 中的格式项替换为指定的 Object 实例的值的文本等效项. Str ...
- WCF服务创建与使用(请求应答模式)
不说废话,直接上代码.以下服务创建是在独立的WCF类库中,若采用WCF应程程序,定义及创建服务代码均相同,但文件名不同,是CalculatorService.svc 第一步,定义服务契约(Servic ...