标签栏使用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 ...
随机推荐
- Android开发学习之路-GSON使用心得(OCR图片识别)
在安卓中解析JSON串可以使用的方法有很多,比如说用官方提供的JSONObject或者谷歌提供的开源库GSON,以及一些第三方开源库. 这里用的是GSON,为了测试方便,借助了一个百度的api,一个图 ...
- 25.按要求编写一个Java应用程序: (1)编写一个矩形类Rect,包含: 两个属性:矩形的宽width;矩形的高height。 两个构造方法: 1.一个带有两个参数的构造方法,用于将width和height属性初化; 2.一个不带参数的构造方法,将矩形初始化为宽和高都为10。 两个方法: 求矩形面积的方法area() 求矩形周长的方法perimeter() (2)通过继承Rect类编写一个具有
package zhongqiuzuoye; //自己写的方法 public class Rect { public double width; public double height; Rect( ...
- 安卓学习进程(2)Android开发环境的搭建
本节将分为五个步骤来完成Android开发环境的部署. 第一步:安装JDK. 第二步:配置Windows上JDK的变量环境 . 第三步:下载安装Eclipse . 第四步:下载安装Androi ...
- WPF自定义控件与样式(15)-终结篇 & 系列文章索引 & 源码共享
系列文章目录 WPF自定义控件与样式(1)-矢量字体图标(iconfont) WPF自定义控件与样式(2)-自定义按钮FButton WPF自定义控件与样式(3)-TextBox & Ric ...
- pojBuy Tickets2828线段树或者树状数组(队列中倒序插队)
这题开始的思路就是模拟:就像数组中插点一样,每一个想买票的人都想往前插队! 但是这样的话肯定TLE, 看了别人的思路之后才恍然大悟! 正解: 将开始的正序插入,变成倒序插入,这样的话,想一想:第 i ...
- maven -- 学习笔记(二)之setting.xml配置说明(备忘)
setting.xml配置说明,learn from:http://pengqb.javaeye.com,http://blog.csdn.net/mypop/article/details/6146 ...
- 利用深搜和宽搜两种算法解决TreeView控件加载文件的问题。
利用TreeView控件加载文件,必须遍历处所有的文件和文件夹. 深搜算法用到了递归. using System; using System.Collections.Generic; using Sy ...
- JavaScript作用域原理(二)——预编译
JavaScript是一种脚本语言, 它的执行过程, 是一种翻译执行的过程.并且JavaScript是有预编译过程的,在执行每一段脚本代码之前, 都会首先处理var关键字和function定义式(函数 ...
- 使用 jQuery & CSS3 实现翻转的作品集滑块
作为 Web 开发人员,我想你一定见过各种各样的图片滑块效果.展示产品或者个人作品的方法有很多,其中之一是使用网格样式的滑块效果.在本教程中,我将分享如何使用 jQuery 和 CSS3 变换实现翻转 ...
- javascript学习总结(一):基础知识。
1 数据类型a.数据类型共有7种,字符串(string).数字(number).布尔(boolean).数组(array).对象(object).Null.Undefined. 其中布尔(逻辑)类型只 ...