标签栏使用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];
}