做项目的时候经常会用到标签,比如说现在很多项目中搜索历史用标签展示 和 选择某个产品的不同属性用标签展示...。网上的有很多封装好的标签,但是作为一个上进的程序员,都希望能有一个自己写的。其实也是一种积累知识的过程。现在的这个标签是根据你的标签的搜字母来排序的。下载地址: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. salesforce 零基础开发入门学习(二)变量基础知识,集合,表达式,流程控制语句

    salesforce如果简单的说可以大概分成两个部分:Apex,VisualForce Page. 其中Apex语言和java很多的语法类似,今天总结的是一些简单的Apex的变量等知识. 有如下几种常 ...

  2. css3使用box-sizing布局

    css3增添了盒模型box-sizing,属性值有下面三个: content-box:默认值,让元素维持W3C的标准盒模型.元素的宽度/高度(width/height)(所占空间)等于元素边框宽度(b ...

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

    with绑定的目的 使用with绑定的格式为data-bind="with:attribute",使用with绑定会将其后所跟的属性看作一个新的上下文进行绑定.with绑定内部的所 ...

  4. Visual-Studio-2015-Cheat-Sheet Visual Studio 2015 快捷键列表

    PDF 文件下载 http://files.cnblogs.com/files/JamesLi2015/Visual-Studio-2015-Cheat-Sheet.pdf

  5. 【WP开发】如何处理溢出的文本

    本文内容适用于Runtime App框架 在用户界面上显示文本,用得严重多的是TextBlock,凡是轻量级的东西都会很常用,TextBlock对于显示简单.少量的文本内容相当适合,不过,在我们考虑要 ...

  6. maven+spring+springMVC+mybatis+dubbox

      milestone 2016612  dubbox+spring+mybatis provider调通

  7. 有关try..catch..finally处理异常的总结

    //看一下下面的程序,你能正确的写出不同的testEx2()方法时,程序的最终打印出来的数据吗....先不要看下面的答案 public class ExceptionTest { public Exc ...

  8. Angular从0到1:function(下)

    1.前言 2.function(下) 2.13.angular.isArray(★★) angular.isArray用于判断对象是不是数组,等价于Array.isArray console.log( ...

  9. 开发人员看测试之TDD和BDD

    前言: 已经数月没有来园子了,写博客贵在坚持,一旦松懈了,断掉了,就很难再拾起来.但是每每看到自己博客里的博文的浏览量每天都在增加,都在无形当中给了我继续写博客的动力.最近这两天有听到Jbehave这 ...

  10. WOW.js – 在页面滚动时展现动感的元素动画效果

    在一些网页上,当你滚动页面的时候会看到各式各样的元素动画效果,非常动感.WOW.js 就是一款帮助你实现这种 CSS 动画效果的插件,很容易定制,你可以改变动画设置喜欢的风格.延迟.长度.偏移和迭代等 ...