效果图: 按钮黑色背景为不可交互 黄色为点击时候的效果 白色为默认颜色

h文件
1// 2// HomeTagView.h 3// YangLand 4// 5// Created by 赵大财 on 16/3/21. 6// Copyright © 2016年 tshiny. All rights reserved. 7// 8 9#import "BaseView.h" 10 11@class HomeTagView; 12 13@protocol HomeTagViewDelegate <NSObject> 14//点击代理方法 15@optional 16- (void)homeTagView:(HomeTagView *)buttonView clickButton:(UIButton *)button; 17 18@end 19 20@interface HomeTagView : BaseView 21 22- (instancetype)initWithTitle:(NSArray *)title mainViewWidth:(CGFloat)mainViewWidth; 23 24@property (nonatomic, weak) id<HomeTagViewDelegate> delegate; 25 26/** 27 *首次获取高度 首次计算 28 */ 29- (CGFloat)heightForHomeTagView:(NSArray *)title mainViewWidth:(CGFloat)mainViewWidth; 30 31/** 32 *二次获取高度 直接获取免计算 33 */ 34- (CGFloat)heightGetHomeTagView; 35 36/** 37 *改变按钮背景颜色 + 是否禁止交互 38 */ 39- (void)changeBtnTitle:(NSArray *)title bgColor:(UIColor *)bgColor borderColor:(UIColor *)borderColor titleColor:(UIColor *)titleColor isAction:(BOOL)isAction; 40 41/** 42 *开启点击选中 再次点击选中取消选中 43 */ 44-(void)clickBtnChangeStateBgColor:(UIColor *)bgColor borderColor:(UIColor *)borderColor titleColor:(UIColor *)titleColor btn:(UIButton *)btn; 45 46@end
m文件
1// 2// HomeTagView.m 3// YangLand 4// 5// Created by 赵大财 on 16/3/21. 6// Copyright © 2016年 tshiny. All rights reserved. 7// 8 9#import "HomeTagView.h" 10 11 12static const CGFloat btnHeight = 30; //按钮高度 13static const CGFloat fontSize = 16; //按钮文字大小 14static const CGFloat btnMarginW = 20; //按钮左右间距 15static const CGFloat btnMarginH = 10; //按钮上下间距 16static const CGFloat btnPadding = 20; //按钮内部边距 17static const CGFloat btnWidthMin = 40; //按钮最新宽度 18static const CGFloat btnBorderWidth = 0.5; //按钮文字边框宽度 19static const CGFloat btnRadius = 4; //按钮圆角 20 21#define TEXT_COLOR [UIColor blackColor] //按钮颜色 22#define BORDER_COLOR [UIColor blackColor] //边框颜色 23#define BTNBG_COLOR [UIColor whiteColor] //按钮的背景色 24 25@interface HomeTagView () 26@property(strong,nonatomic) NSMutableArray *btnArr; //全部按钮 27@property(strong,nonatomic) UIButton *prevClickBtn; // 28@property(strong,nonatomic) NSArray *changeBtnArr; //已经被改变了颜色的按钮 29@property(strong,nonatomic) NSArray *btnFrameArr; //按钮frame 30 31@end 32 33@implementation HomeTagView 34 35- (instancetype)initWithTitle:(NSArray *)title mainViewWidth:(CGFloat)mainViewWidth { 36 self=[super init]; 37 if (self) { 38 _btnArr = [NSMutableArray array]; 39 NSArray * frame = [self returnBtnFrame:title :mainViewWidth]; 40 int i = 0; 41 for (NSArray *arr in frame) { 42 UIButton *cbtn=[self createBtn:title[i]]; 43 cbtn.x = [arr[0] floatValue]; //这里UIView扩展 44 cbtn.y = [arr[1] floatValue]; 45 cbtn.width = [arr[2] floatValue]; 46 cbtn.height = [arr[3] floatValue]; 47 [self addSubview:cbtn]; 48 [_btnArr addObject:cbtn]; 49 i++; 50 } 51 } 52 return self; 53} 54 55- (CGFloat)heightForHomeTagView:(NSArray *)title mainViewWidth:(CGFloat) mainViewWidth { 56 CGFloat height; 57 if(title.count>0){ 58 NSArray *frame = [self returnBtnFrame:title :mainViewWidth]; 59 NSArray *frameRow = [frame lastObject]; 60 height= [frameRow[1] floatValue]+btnHeight; 61 }else{ 62 return 0; 63 } 64 return height; 65} 66 67- (CGFloat)heightGetHomeTagView { 68 NSArray *frameRow = [_btnFrameArr lastObject]; 69 CGFloat height = [frameRow[1] floatValue]+btnHeight; 70 return height; 71} 72 73- (NSArray *)returnBtnFrame:(NSArray *)title : (CGFloat) mainViewWidth { 74 int row = 0; //行数 75 CGFloat currentX = 0; //当前X 76 CGFloat currentY = 0; //当前Y 77 CGFloat preBtnW = 0; //上一个按钮宽度 78 NSMutableArray * frameArr = [NSMutableArray array]; 79 for (int i=0; i<title.count; i++) { 80 NSString * btnStr = title[i]; 81 CGSize titleSize =[btnStr sizeWithFontWidth:[UIFont systemFontOfSize:fontSize]]; //这里是计算大小的扩展 82 CGFloat btnWidth =titleSize.width+btnPadding; 83 btnWidth<btnWidthMin?btnWidth=btnWidthMin:btnWidthMin; 84 if(i == 0 ){ 85 currentX = 0; 86 preBtnW = btnWidth; 87 }else { 88 if(preBtnW>mainViewWidth||(currentX+btnMarginW+btnWidth+preBtnW)>mainViewWidth){ 89 row++; 90 currentX=0; 91 preBtnW = btnWidth; 92 }else{ 93 currentX += (btnMarginW+preBtnW); 94 preBtnW = btnWidth; 95 } 96 } 97 currentY = row*(btnHeight+btnMarginH); 98 NSArray *arr = @[@(currentX),@(currentY),@(btnWidth),@(btnHeight)]; 99 [frameArr addObject:arr]; 100 } 101 _btnFrameArr = frameArr; 102 return frameArr; 103} 104 105- (UIButton*)createBtn:(NSString *)title { 106 UIButton * btn =[UIButton buttonWithType:UIButtonTypeRoundedRect]; 107 btn.backgroundColor = BTNBG_COLOR; 108 [btn setTitle:title forState:UIControlStateNormal]; 109 btn.layer.cornerRadius = btnRadius; 110 btn.layer.masksToBounds = YES; 111 UIColor *borderColor = BORDER_COLOR; 112 btn.layer.borderColor = borderColor.CGColor; 113 btn.layer.borderWidth = btnBorderWidth; 114 [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside]; 115 [btn setTitleColor:TEXT_COLOR forState:UIControlStateNormal]; 116 return btn; 117} 118 119- (void)changeBtnTitle:(NSArray *)title bgColor:(UIColor *)bgColor borderColor:(UIColor *)borderColor titleColor:(UIColor *)titleColor isAction:(BOOL)isAction{ 120 _changeBtnArr = title; 121 for (UIButton *btn in _btnArr) { 122 if([title containsObject:btn.titleLabel.text]){ 123 btn.backgroundColor = bgColor; 124 [btn setTitleColor:titleColor forState:UIControlStateNormal]; 125 UIColor *borderColor = bgColor; 126 btn.layer.borderColor = borderColor.CGColor; 127 btn.userInteractionEnabled = isAction; 128 } 129 } 130} 131 132- (void)clickBtnChangeStateBgColor:(UIColor *)bgColor borderColor:(UIColor *)borderColor titleColor:(UIColor *)titleColor btn:(UIButton *)btn { 133 if(btn == _prevClickBtn){ 134 _prevClickBtn = nil; 135 btn.backgroundColor = BTNBG_COLOR; 136 UIColor *borderColor = BORDER_COLOR; 137 btn.layer.borderColor = borderColor.CGColor; 138 [btn setTitleColor:TEXT_COLOR forState:UIControlStateNormal]; 139 }else{ 140 for (UIButton *btn in _btnArr) { 141 if(![_changeBtnArr containsObject:btn.titleLabel.text]){ 142 btn.backgroundColor = BTNBG_COLOR; 143 UIColor *borderColor = BORDER_COLOR; 144 btn.layer.borderColor = borderColor.CGColor; 145 [btn setTitleColor:TEXT_COLOR forState:UIControlStateNormal]; 146 } 147 } 148 btn.backgroundColor = bgColor; 149 [btn setTitleColor:titleColor forState:UIControlStateNormal]; 150 btn.layer.borderColor = borderColor.CGColor; 151 _prevClickBtn = btn; 152 } 153} 154 155 156- (void)btnClick:(UIButton *)btn { 157 if ([_delegate respondsToSelector:@selector(homeTagView:clickButton:)]) { 158 [_delegate homeTagView:self clickButton:btn ]; 159 } 160 161} 162@end