标签栏使用Demo二
//
// PHTagViewFrame.m
// 标签的使用二
//
// Created by 123 on 16/9/6.
// Copyright © 2016年 彭洪. All rights reserved.
//
/**
* 计算多个标签的位置 标签根据文字自适应宽度 每行超过的宽度平均分配给每个标签 每个标签左右对齐
*
* @return
*/
#import "PHTagViewFrame.h"
#define padding10 10
@implementation PHTagViewFrame
- (id)init {
self = [super init];
if (self) {
_tagsFrames = [[NSMutableArray alloc] init];
_tagsMinPadding = padding10;
_tagsMargin = padding10;
_tagsLineSpaceing = padding10;
}
return self;
}
- (void)setTagsArray:(NSArray *)tagsArray {
_tagsArray = tagsArray;
CGFloat btnX = _tagsMargin;
CGFloat btnW = 0;
CGFloat nextWidth = 0;//下一个标签宽度
CGFloat moreWidth = 0;//每一行多出来的宽度
//每一行的最后一个tag的索引数组和每一行多出来的宽度的数组
NSMutableArray *lastIndexs = [[NSMutableArray alloc] init];
NSMutableArray *moreWidths = [[NSMutableArray alloc] init];
for (int i=0; i<tagsArray.count; i++) {
btnW = [self sizeWithText:tagsArray[i] font:TagTitleFont].width + _tagsMinPadding *2;
if (i <tagsArray.count-1) {
nextWidth = [self sizeWithText:tagsArray[i+1] font:TagTitleFont].width + _tagsMinPadding*2;
}
CGFloat nextBtnX = btnX+btnW + _tagsMargin;
//如果下一个按钮 标签最右边则换行
if ((nextBtnX + nextWidth)>(SCREEN_W-_tagsMargin)) {
//计算超过的宽度
moreWidth = SCREEN_W - nextBtnX;
[lastIndexs addObject:[NSNumber numberWithInteger:i]];
[moreWidths addObject:[NSNumber numberWithFloat:moreWidth]];
btnX = _tagsMargin;
}
else {
btnX += (btnW + _tagsMargin);
}
//如果是最后一个且数组中没有 则加入数组中
if (i == tagsArray.count - 1) {
if (![lastIndexs containsObject:[NSNumber numberWithInt:i]]) {
[lastIndexs addObject:[NSNumber numberWithInt:i]];
[moreWidths addObject:[NSNumber numberWithFloat:0]];
}
}
}
NSInteger location = 0;//截取的位置
NSInteger length = 0;//截取的长度
CGFloat averageW = 0;//多出来的平均宽度
CGFloat tagW = 0;
CGFloat tagH = 30;
for (int i=0; i<lastIndexs.count; i++) {
NSInteger lastIndex = [lastIndexs[i]integerValue];
if (i == 0) {
length = lastIndex + 1;
}
else {
length = [lastIndexs[i]integerValue]-[lastIndexs[i-1]integerValue];
}
//从数组中截取每一行的数组
NSArray *newArr = [tagsArray subarrayWithRange:NSMakeRange(location, length)];
location = lastIndex +1;
averageW = [moreWidths[i]floatValue]/newArr.count;
CGFloat tagX = _tagsMargin;
CGFloat tagY = _tagsLineSpaceing + (_tagsLineSpaceing + tagH)*i;
for (int j=0; j<newArr.count; j++) {
tagW = [self sizeWithText:newArr[j] font:TagTitleFont].width + _tagsMinPadding *2 + averageW;
CGRect btnF = CGRectMake(tagX, tagY, tagW, tagH);
[_tagsFrames addObject:NSStringFromCGRect(btnF)];
tagX += (tagW + _tagsMargin);
}
}
_tagsHeight = (tagH + _tagsLineSpaceing) * lastIndexs.count + _tagsLineSpaceing;
}
/**
* 单行文本数据获取宽高
*/
- (CGSize)sizeWithText:(NSString *)text font:(UIFont *)font {
NSDictionary *attrs = @{NSFontAttributeName:font};
return [text sizeWithAttributes:attrs];
}
@end
//
// PHTagViewFrame.h
// 标签的使用二
//
// Created by 123 on 16/9/6.
// Copyright © 2016年 彭洪. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#define SCREEN_W [UIScreen mainScreen].bounds.size.width
#define SCREEN_H [UIScreen mainScreen].bounds.size.height
#define TagTitleFont [UIFont systemFontOfSize:13]
@interface PHTagViewFrame : NSObject
/**
* 标签名字数组
*/
@property (nonatomic,strong) NSArray *tagsArray;
/**
* 标签frame数组
*/
@property (nonatomic,strong) NSMutableArray *tagsFrames;
/**
* 标签高度
*/
@property (nonatomic,assign) CGFloat tagsHeight;
/**
* 标签间距
*/
@property (nonatomic,assign) CGFloat tagsMargin;
/**
* 标签行间距
*/
@property (nonatomic,assign) CGFloat tagsLineSpaceing;
/**
* 标签最小内边距
*/
@property (nonatomic,assign) CGFloat tagsMinPadding;
@end
//
// PHTagView.h
// 标签的使用二
//
// Created by 123 on 16/9/6.
// Copyright © 2016年 彭洪. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "PHTagViewFrame.h"
#define TextColor [UIColor colorWithRed:51.0/255.0 green:51.0/255.0 blue:51.0/255.0 alpha:1.0]
#define UIColorRGBA(r,g,b,a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:(a)]
@class PHTagViewFrame;
@protocol TagViewDelegate <NSObject>
- (void)tagView:(NSArray *)tagArray;
@end
@interface PHTagView : UIView
{
//储存选中按钮的tag
NSMutableArray *selectedBtnList;
}
@property (nonatomic,weak) id<TagViewDelegate>delegate;
/** 是否能选中 需要在frame钱调用 默认yes */
@property (nonatomic,assign) BOOL clickBool;
/** 未选中边框大小 需要在frame前调用 默认0.5 */
@property (nonatomic,assign) CGFloat borderSize;
/** frame */
@property (nonatomic,strong) PHTagViewFrame *tagsFrame;
/** 选中背景颜色 默认白色 */
@property (nonatomic,strong) UIColor *clickBackgroundColor;
/** 选中字体颜色 默认 */
@property (nonatomic,strong) UIColor *clickTitleColor;
/** 多选选中 默认未选中 */
@property (nonatomic,strong) NSArray *clickArray;
/** 单选选中 默认未选中 */
@property (nonatomic,strong) NSString *clickString;
/** 选中边框大小 默认0.5 */
@property (nonatomic,assign) CGFloat clickBorderSize;
/** 1-多选 0-单选 默认单选 */
@property (nonatomic,assign) NSInteger clickStart;
@end
//
// PHTagView.m
// 标签的使用二
//
// Created by 123 on 16/9/6.
// Copyright © 2016年 彭洪. All rights reserved.
//
#import "PHTagView.h"
@implementation PHTagView
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
selectedBtnList = [[NSMutableArray alloc] init];
self.clickBackgroundColor = [UIColor whiteColor];
self.clickTitleColor = TextColor;
self.clickArray = nil;
self.clickBool = YES;
self.borderSize = 0.5;
self.clickBorderSize = 0.5;
}
return self;
}
- (void)setTagsFrame:(PHTagViewFrame *)tagsFrame {
_tagsFrame = tagsFrame;
for (NSInteger i=0; i<tagsFrame.tagsArray.count; i++) {
UIButton *tagsBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[tagsBtn setTitle:tagsFrame.tagsArray[i] forState:UIControlStateNormal];
[tagsBtn setTitleColor:TextColor forState:UIControlStateNormal];
tagsBtn.titleLabel.font = TagTitleFont;
tagsBtn.tag = i;
tagsBtn.backgroundColor = [UIColor whiteColor];
// [self ma]
}
}
#pragma mark 选中背景颜色
- (void)setClickBackgroundColor:(UIColor *)clickBackgroundColor {
if (_clickBackgroundColor != _clickBackgroundColor) {
_clickBackgroundColor = clickBackgroundColor;
}
}
#pragma mark 选中字体颜色
- (void)setClickTitleColor:(UIColor *)clickTitleColor {
if (_clickTitleColor != clickTitleColor) {
_clickTitleColor = clickTitleColor;
}
}
#pragma mark 能否被选中
- (void)setClickBool:(BOOL)clickBool {
_clickBool = clickBool;
}
#pragma mark 选中边框大小
- (void)setBorderSize:(CGFloat)borderSize {
if (_borderSize != borderSize) {
_borderSize = borderSize;
}
}
#pragma mark 选中边框大小
- (void)setClickBorderSize:(CGFloat)clickBorderSize {
if (_clickBorderSize != clickBorderSize) {
_clickBorderSize = clickBorderSize;
}
}
#pragma mark 默认选择 单选
- (void)setClickString:(NSString *)clickString {
if (_clickString != clickString) {
_clickString = clickString;
}
if ([_tagsFrame.tagsArray containsObject:_clickString]) {
NSInteger index = [_tagsFrame.tagsArray indexOfObject:_clickString];
[self clickString:index];
}
}
#pragma mark 默认选则 多选
- (void)setClickArray:(NSArray *)clickArray {
if (_clickArray != clickArray) {
_clickArray = clickArray;
}
for (NSString *string in clickArray) {
if ([_tagsFrame.tagsArray containsObject:string]) {
NSInteger index = [_tagsFrame.tagsArray indexOfObject:string];
NSString *x = [[NSString alloc] initWithFormat:@"%ld",(long)index];
[self clickArray:x];
}
}
}
#pragma mark 单选
- (void)clickString:(NSInteger)index {
UIButton *btn ;
for (id obj in self.subviews) {
if ([obj isKindOfClass:[UIButton class]]) {
btn = (UIButton *)obj;
if (btn.tag == index) {
btn.backgroundColor = [UIColor whiteColor];
[btn setTitleColor:_clickTitleColor forState:UIControlStateNormal];
[self makeCorner:_clickBorderSize view:btn color:_clickTitleColor];
[_delegate tagView:@[[NSString stringWithFormat:@"%ld",(long)index]]];
}
else {
btn.backgroundColor = [UIColor whiteColor];
[btn setTitleColor:TextColor forState:UIControlStateNormal];
[self makeCorner:_borderSize view:btn color:UIColorRGBA(221, 221, 221, 1)];
}
}
}
}
#pragma mark 多选
- (void)clickArray:(NSString *)index {
UIButton *btn;
for (id obj in self.subviews) {
if ([obj isKindOfClass:[UIButton class]]) {
btn = (UIButton *)obj;
if (btn.tag == [index integerValue]) {
btn.backgroundColor = [UIColor whiteColor];
if ([selectedBtnList containsObject:index]) {
[btn setTitleColor:TextColor forState:UIControlStateNormal];
[self makeCorner:_clickBorderSize view:btn color:_clickTitleColor];
[selectedBtnList addObject:index];
}
else {
[btn setTitleColor:_clickTitleColor forState:UIControlStateNormal];
[self makeCorner:_clickBorderSize view:btn color:_clickTitleColor];
[selectedBtnList addObject:index];
}
[_delegate tagView:selectedBtnList];
}
}
}
}
//设置角标
- (void)makeCorner:(CGFloat)corner view:(UIView *)view color:(UIColor *)color {
CALayer *filesLayer = [view layer];
filesLayer.borderColor = [color CGColor];
filesLayer.borderWidth = corner;
}
- (void)tagsBtn:(UIButton *)sender {
if (self.clickStart == 0) {
//单选
[self clickString:sender.tag];
}
else {
//多选
NSString *x = [[NSString alloc] initWithFormat:@"%ld",(long)sender.tag];
[self clickArray:x];
}
}
@end
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = UIColorRGBA(238, 238, 238, 1);
NSArray *array = @[@"code4app",@"轻音少女",@"花季少女",@"我们仍未知道那天所看见的花的名字",@"华语",@"花有重开日",@"空之境界"];
PHTagViewFrame *frame = [[PHTagViewFrame alloc] init];
frame.tagsMinPadding = 4;
frame.tagsMargin = 10;
frame.tagsLineSpaceing = 10;
frame.tagsArray = array;
PHTagView *tagView = [[PHTagView alloc] initWithFrame:CGRectMake(0, 30, SCREEN_W, frame.tagsHeight)];
tagView.clickBool = YES;
tagView.borderSize = 0.5;
tagView.clickBorderSize = 0.5;
tagView.tagsFrame = frame;
tagView.clickBackgroundColor = BACKGROUNDCOLOR;
tagView.clickTitleColor = BACKGROUNDCOLOR;
tagView.clickStart = 0;
tagView.clickString = @"华语";//单选 tagView.clickStart 为0
// tagView.clickArray = @[@"误解向",@"我们仍未知道那天所看见的花的名字"];//多选 tagView.clickStart 为1
tagView.delegate = self;
[self.view addSubview:tagView];
}
标签栏使用Demo二的更多相关文章
- baidumap demo(二)
接口说明 百度地图API提供的搜索服务包括:POI检索,多关键字检索,公交方案检索,驾车路线检索,步行路线检索,地理编码,反地理编码,公交详情检索,在线建议查询,短串分享. 所有检索请求接口均为异步接 ...
- Tornado 网站demo 二
连接数据库 methods 中建立一个文件 db.py 分别建立起连接对象和游标对象 #!/usr/bin/env Python # coding=utf-8 import pymysql conn ...
- ios 团购信息客户端demo(二)
接上一篇,这篇我们对我们的客户端加入KissXML,MBProgressHUD,AQridView这几个库,首先我们先加入KissXML,这是XML解析库,支持Xpath,可以方便添加更改任何节点.先 ...
- 使用Flexible适配移动端html页面 - demo记录
前段时间看了大神的博客文章[使用Flexible实现手淘H5页面的终端适配](地址:http://www.w3cplus.com/mobile/lib-flexible-for-html5-layou ...
- TensorFlow 在android上的Demo(1)
转载时请注明出处: 修雨轩陈 系统环境说明: ------------------------------------ 操作系统 : ubunt 14.03 _ x86_64 操作系统 内存: 8GB ...
- 第7篇 ORACLE EBS DEMO虚拟机环境的安装
ERP信息系统的实施不仅要求懂得道理方面的知识,更要侧重于应用实践.为了有一个稳定的测试环境.初学者可以自己搭建一个EBS DEMO环境.本节介绍EBS DEMO环境虚拟机的安装.一. 安装前的准备( ...
- echarts标准饼图(一)——基本配置demo
echarts标准饼图解读共分为四部分, 一.基本配置demo 二.标题(title)配置 三.提示框(tooltip)配置 四.图例(legend)配置 五.系列列表(series )配置 下面是一 ...
- spring4.0之二:@Configuration的使用
从Spring3.0,@Configuration用于定义配置类,可替换xml配置文件,被注解的类内部包含有一个或多个被@Bean注解的方法,这些方法将会被AnnotationConfigApplic ...
- 转载:二次指数平滑法求预测值的Java代码
原文地址: http://blog.csdn.net/qustmeng/article/details/52186378?locationNum=4&fps=1 import java.uti ...
随机推荐
- React(一)基础点
prop实例 <div id="app"></div> <script src="bower_components/react/react. ...
- maven+svn忽略提交到svn的文件
- PCurve - Curve on Surface
PCurve - Curve on Surface eryar@163.com Abstract. 本文通过给出曲面上曲线PCurve的定义来对OpenCascade中的Curve On Surfac ...
- Geometry Curve of OpenCascade BRep
Geometry Curve of OpenCascade BRep eryar@163.com 摘要Abstract:几何曲线是参数表示的曲线 ,在边界表示中其数据存在于BRep_TEdge中,BR ...
- WCF传输1-你是否使用过压缩或Json序列化?
1.当遇到需要传输大量数据时,怎么样传输数据? 2.压缩数据有哪几种常见的方式? 问题1解答:通过压缩来传输数据 问题2解答: (1)WCF自带的压缩方式 (2)自定义WCF binding进行压缩 ...
- 那些年,在AngularJS的路上遇到的坑
使用AngularJS这么久以来,遇到过一些坑,之前一直没有以书面的形式整理下,现在好好总结下,以供后面查备. 一.angular scope 在ng-if与ng-show下的区别(两者作用域的差别) ...
- 如何配置Hyper-V的虚拟机通过主机网络上网 (NAT)
前言 最近开始在Windows 8 上面直接使用Hyper-V的技术来建立虚拟环境进行开发和测试,这样免去了再安装额外软件的需要.在实际使用的时候,尤其是配置网络共享的时候,遇到些问题,与其他一些虚拟 ...
- ios见习之-UISearchbar+tableview实现自动搜索自带提示
当做搜索时常常希望能在输入的时候出现搜索关键字,如下效果
- 配置Hibernate二级缓存时,不能初始化RegionFactory的解决办法
配置Hibernate 二级缓存时,出现以下bug提示: SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder&quo ...
- Mysql在高并发情况下,防止库存超卖而小于0的解决方案
背景: 本人上次做申领campaign的PHP后台时,因为项目上线后某些时段同时申领的人过多,导致一些专柜的存货为负数(<0),还好并发量不是特别大,只存在于小部分专柜而且一般都是-1的状况,没 ...