标签栏使用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 ...
随机推荐
- js显示yyyy年mm日dd天 星期几 的格式日期
js代码: <script type="text/javascript"> var today = new Date(); var week; //存储星期几 var ...
- 《PHP Manual》阅读笔记3 —— 类与对象
1.PHP 中的所有函数和类都具有全局作用域,可以定义在一个函数之内而在之外调用,反之亦然. PHP 不支持函数重载,也不可能取消定义或者重定义已声明的函数. 当一个函数是有条件被定义时,必须在调用函 ...
- Java 多态——与C++的比较
学习了Java和C++之后,由于长期不使用C++,而java的基础知识掌握不牢,现在已经搞不清java多态了.现在先来谈谈java多态,稍后有时间再更新C++的多态,并进行比较~ 一. Java的多态 ...
- 查看 table,view,sp的定义
1, 查看用户创建的Proc,View, UDF,trigger 的定义 sys.sql_modules Returns a row for each object that is an SQL la ...
- LLBL Gen Pro 4.2 Lite 免费的对象关系映射开发框架与工具
LLBL Gen Pro是一款优秀的对象关系映射开发框架,自2003年发布以来,一直有广泛的客户群.LLBL Gen Pro有几个标志性的版本,2.5/2.6是一个很稳定的版本,公司的一些旧的项目仍然 ...
- mongodb 关系、引用、覆盖索引查询
一.关系 MongoDB 的关系表示多个文档之间在逻辑上的相互联系.文档间可以通过嵌入和引用来建立联系.MongoDB 中的关系可以是:1对1,1对多,多对1,多对多. 一个用户可以用多个地址,这是典 ...
- C#搭建足球赛事资料库与预测平台(1) 基本介绍
本博客所有文章分类的总目录:[总目录]本博客博文总目录-实时更新 开源C#彩票数据资料库系列文章总目录:[目录]C#搭建足球赛事资料库与预测平台与彩票数据分析目录 去年4月到现在,一年 ...
- CSS 魔法系列:纯 CSS 绘制图形(各种形状的钻石)
我们的网页因为 CSS 而呈现千变万化的风格.这一看似简单的样式语言在使用中非常灵活,只要你发挥创意就能实现很多比人想象不到的效果.特别是随着 CSS3 的广泛使用,更多新奇的 CSS 作品涌现出来. ...
- 微信小程序中rpx与rem单位使用
原作者: 小小小 来自: 授权地址 本文讲解rpx和rem应用于微信小程序,如果你还没有入门,建议先从下面看起: 微信小程序官方文档web app变革之remrpx单位官方文档rpx单位基础介绍 ...
- eclipse中关联文件设置方法
在前几次的试验中,只是做了处于应用程序最上层的界面设计,其实还不知程序在运行过程中到底调用了哪些函数,这些函数是怎么实现的,由于搭建环境时没有进行文件关联,所以在环境中无法实现ctrl键+左击鼠标的方 ...