//
// WSQAdaptiveLable.h
// Test
//
// Created by Crazy Wang on 2018/11/12.
// Copyright © 2018年 Crazy Wang. All rights reserved.
//
#import <UIKit/UIKit.h>
//按鈕整體 居左 居中 居右 的樣式
typedef NS_ENUM(NSInteger, WSQAdaptiveLableType) {
WSQAdaptiveLableTypeLeft =0, // letf
WSQAdaptiveLableTypeCenter =1, //center
WSQAdaptiveLableTypeRight =2, // right
};
//單選還是多選
typedef NS_ENUM(NSInteger, WSQAdaptiveLableSingleOrMultiple) {
WSQAdaptiveLableSingle =0, // 單選
WSQAdaptiveLableMultiple =1, //多選
};
@interface WSQAdaptiveLable : UIView
/**
* 初始化
*
* @param frame frame
* @param tagArray 標籤數組
* @param type 標籤居左還是居右
* @param singleOrMultiple 單選還是多選
*
*/
- (instancetype)initWithFrame:(CGRect)frame tagArray:(NSArray*)tagArray andType:(WSQAdaptiveLableType)type andSingleOrMultiple:(WSQAdaptiveLableSingleOrMultiple)singleOrMultiple;
/** 如果不設置以下屬性,則使用默認 樣式*/
// 標籤數組
@property (nonatomic,strong) NSArray* tagArray;
//按鈕的高度
@property (nonatomic,assign) CGFloat btnH;
// 選中標籤文字顏色
@property (nonatomic,strong) UIColor* textColorSelected;
// 默認標籤文字顏色
@property (nonatomic,strong) UIColor* textColorNormal;
// 選中標籤背景顏色
@property (nonatomic,strong) UIColor* backgroundColorSelected;
// 默認標籤背景顏色
@property (nonatomic,strong) UIColor* backgroundColorNormal;
// 選中標籤背景邊框色
@property (nonatomic,strong) UIColor* borderColorSelected;
// 默認標籤背景邊框色
@property (nonatomic,strong) UIColor* borderColorNormal;
//設置默認字體型號與大小
@property (nonatomic,strong) UIFont* textFontNormal;
//設置選中字體型號與大小
@property (nonatomic,strong) UIFont* textFontSelected;
// 標籤總高度 獲取這個屬性能取得整個view所需得的高度
@property (nonatomic,assign) CGFloat selfHeight;
// 選擇的標籤 獲取這個屬性能取得選中的按鈕的下標數組
@property (nonatomic,strong) NSMutableArray *selseData;
@end
//
// WSQAdaptiveLable.m
// Test
//
// Created by Crazy Wang on 2018/11/12.
// Copyright © 2018年 Crazy Wang. All rights reserved.
//
#import "WSQAdaptiveLable.h"
@interface WSQAdaptiveLable()
{
WSQAdaptiveLableType tageType;
WSQAdaptiveLableSingleOrMultiple sigMul;
}
@end
@implementation WSQAdaptiveLable
/**
* 初始化
*
* @param frame frame
* @param tagArray 標籤數組
*
*
*/
- (instancetype)initWithFrame:(CGRect)frame tagArray:(NSArray*)tagArray andType:(WSQAdaptiveLableType)type andSingleOrMultiple:(WSQAdaptiveLableSingleOrMultiple)singleOrMultiple{
self = [super initWithFrame:frame];
if (self) {
_tagArray = tagArray;
tageType = type;
sigMul = singleOrMultiple;
_selseData = [NSMutableArray new];
[self setUp];
}
return self;
}
// 初始化默認的屬性
- (void)setUp{
// 默認顏色
_btnH = 20.0;
_textColorNormal = [UIColor darkGrayColor];
_textColorSelected = [UIColor redColor];
_backgroundColorSelected = [UIColor whiteColor];
_backgroundColorNormal = [UIColor whiteColor];
_borderColorNormal = [UIColor lightGrayColor];
_borderColorSelected = [UIColor redColor];
_textFontNormal = [UIFont systemFontOfSize:14.0f];
_textFontSelected = [UIFont systemFontOfSize:14.0f];
// 創建標籤按鈕
[self createTagButton];
}
// 重寫set屬性
- (void)setTagArray:(NSMutableArray *)tagArray{
_tagArray = tagArray;
// 重新創建標籤
[self resetTagButton];
}
// 重寫set屬性
- (void)setBtnH:(CGFloat)btnH{
_btnH = btnH;
// 重新創建標籤
[self resetTagButton];
}
- (void)setTextColorSelected:(UIColor *)textColorSelected{
_textColorSelected = textColorSelected;
// 重新創建標籤
[self resetTagButton];
}
- (void)setTextColorNormal:(UIColor *)textColorNormal{
_textColorNormal = textColorNormal;
// 重新創建標籤
[self resetTagButton];
}
- (void)setBackgroundColorSelected:(UIColor *)backgroundColorSelected{
_backgroundColorSelected = backgroundColorSelected;
// 重新創建標籤
[self resetTagButton];
}
- (void)setBackgroundColorNormal:(UIColor *)backgroundColorNormal{
_backgroundColorNormal = backgroundColorNormal;
// 重新創建標籤
[self resetTagButton];
}
- (void)setTextFontNormal:(UIFont *)textFontNormal{
_textFontNormal = textFontNormal;
// 重新創建標籤
[self resetTagButton];
}
- (void)setTextFontSelected:(UIFont *)textFontSelected{
_textFontSelected = textFontSelected;
// 重新創建標籤
[self resetTagButton];
}
#pragma mark - 清空當前的View上所有子控制器
// 重新創建標籤
- (void)resetTagButton{
// 移除之前的標籤
for (UIView* sub in self.subviews) {
[sub removeFromSuperview];
}
// 重新創建標籤
[self createTagButton];
}
// 創建標籤按鈕
- (void)createTagButton{
/* 初始化數據 */
//記錄判斷是否需要創建標籤的父view
CGFloat backBseH = 0.0;
//爲了記錄每行按鈕的背景view
NSInteger heng = 0;
//整個self的高度
_selfHeight = 0.0;
// 距離左邊距
CGFloat leftX = 0;
// 距離上邊距
CGFloat topY = 10;
// 按鈕左右間隙
CGFloat marginX = 10;
// 按鈕上下間隙
CGFloat marginY = 10;
// 文字左右間隙
CGFloat fontMargin = 10;
//開始創建標籤按鈕
for (int i = 0; i < _tagArray.count; i++) {
UIButton* btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(0 + leftX, 0, 100, _btnH);
btn.tag = 100+i;
// 按鈕文字
[btn setTitle:_tagArray[i] forState:UIControlStateNormal];
//按鈕文字默認樣式
NSMutableAttributedString* btnDefaultAttr = [[NSMutableAttributedString alloc]initWithString:btn.titleLabel.text];
// 文字大小
[btnDefaultAttr addAttribute:NSFontAttributeName value:_textFontNormal range:NSMakeRange(0, btn.titleLabel.text.length)];
// 默認顏色
[btnDefaultAttr addAttribute:NSForegroundColorAttributeName value:self.textColorNormal range:NSMakeRange(0, btn.titleLabel.text.length)];
[btn setAttributedTitle:btnDefaultAttr forState:UIControlStateNormal];
// 默認背景顏色
[btn setBackgroundImage:[self imageWithColor:self.backgroundColorNormal] forState:UIControlStateNormal];
// 選中字體顏色
NSMutableAttributedString* btnSelectedAttr = [[NSMutableAttributedString alloc]initWithString:btn.titleLabel.text];
// 選中顏色
[btnSelectedAttr addAttribute:NSForegroundColorAttributeName value:self.textColorSelected range:NSMakeRange(0, btn.titleLabel.text.length)];
// 選中文字大小
[btnSelectedAttr addAttribute:NSFontAttributeName value:_textFontSelected range:NSMakeRange(0, btn.titleLabel.text.length)];
[btn setAttributedTitle:btnSelectedAttr forState:UIControlStateSelected];
// 選中背景顏色
[btn setBackgroundImage:[self imageWithColor:self.backgroundColorSelected] forState:UIControlStateSelected];
// 圓角
btn.layer.cornerRadius = btn.frame.size.height / 2.f;
btn.layer.masksToBounds = YES;
// 邊框
btn.layer.borderColor = self.borderColorNormal.CGColor;
btn.layer.borderWidth = 0.5;
// 設置按鈕的邊距、間隙
[self setTagButtonMargin:btn fontMargin:fontMargin];
// 處理換行
if (btn.frame.origin.x + btn.frame.size.width + marginX > self.frame.size.width) {
// 換行
topY += _btnH + marginY;
// 重置
leftX = 0;
btn.frame = CGRectMake(leftX, 0, 100, _btnH);
// 設置按鈕的邊距、間隙
[self setTagButtonMargin:btn fontMargin:fontMargin];
};
// 重置按鈕高度
CGRect frame = btn.frame;
frame.size.height = _btnH;
btn.frame = frame;
//----- 選中事件
[btn addTarget:self action:@selector(selectdButton:) forControlEvents:UIControlEventTouchUpInside];
btn.tag = 250 +i;
leftX += btn.frame.size.width + marginX;
//如果不等就創建一個新的view
if (backBseH != topY) {
//是爲了判斷在同一行上的
backBseH = topY;
NSLog(@"backBseH:%f topY:%f",backBseH,topY);
heng = heng +1;
NSLog(@"heng:%ld",heng);
UIView *seBackView = [[UIView alloc]initWithFrame:CGRectMake(0, backBseH, self.bounds.size.width, _btnH + marginY)];
seBackView.tag = 5000+heng;
[self addSubview:seBackView];
seBackView.backgroundColor = [UIColor clearColor];
}
//設置按鈕view的相對self的位置
[self reSetVewFreme:btn andHeng:heng andbackH:backBseH andWeith:leftX -marginX];
if (i ==_tagArray.count -1 ) {//取最後一個按鈕的高度
_selfHeight = CGRectGetHeight(btn.frame) +topY;
}
}
//返回整個self的高度
_selfHeight = _selfHeight+marginY;
}
// 設置按鈕是居中還是居左還是居右
- (void)reSetVewFreme:(UIButton *)btn andHeng:(NSInteger)heng andbackH:(CGFloat)backBseh andWeith:(CGFloat)weth {
UIView *view = (UIView *) [self viewWithTag:5000+heng];
[view addSubview:btn];
if (tageType == WSQAdaptiveLableTypeLeft) {
view.frame =CGRectMake(0.0, backBseh , weth, CGRectGetMaxY(btn.frame) );
}else if (tageType == WSQAdaptiveLableTypeCenter){
view.frame =CGRectMake((self.bounds.size.width -weth)/2.0, backBseh , weth, CGRectGetMaxY(btn.frame) );
}else if (tageType == WSQAdaptiveLableTypeRight){
view.frame =CGRectMake(self.bounds.size.width -weth, backBseh , weth, CGRectGetMaxY(btn.frame));
}
}
// 設置按鈕的邊距、間隙
- (void)setTagButtonMargin:(UIButton*)btn fontMargin:(CGFloat)fontMargin{
// 按鈕自適應
[btn sizeToFit];
// 重新計算按鈕文字左右間隙
CGRect frame = btn.frame;
frame.size.width += fontMargin*2;
btn.frame = frame;
}
// 根據顏色生成UIImage
- (UIImage*)imageWithColor:(UIColor*)color{
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
// 開始畫圖的上下文
UIGraphicsBeginImageContext(rect.size);
// 設置背景顏色
[color set];
// 設置填充區域
UIRectFill(CGRectMake(0, 0, rect.size.width, rect.size.height));
// 返回UIImage
UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
// 結束上下文
UIGraphicsEndImageContext();
return image;
}
#pragma mark - 點擊事件
// 標籤按鈕點擊事件
- (void)selectdButton:(UIButton*)btn{
if (sigMul == WSQAdaptiveLableMultiple) {//多選
btn.selected = !btn.selected;
//返回選中的標籤數組下標
if (btn.selected) {
[self.selseData addObject:[NSString stringWithFormat:@"%ld",btn.tag-250]];
btn.layer.borderColor = self.borderColorSelected.CGColor;
}else{
for (int i = 0; i <self.selseData.count; i ++) {
if ([self.selseData[i] isEqualToString:[NSString stringWithFormat:@"%ld",btn.tag-250]]) {
[self.selseData removeObjectAtIndex:i];
}
}
btn.layer.borderColor = self.borderColorNormal.CGColor;
}
NSLog(@"%@,%ld",self.selseData,self.selseData.count);
}else{//單選
self.selseData = [NSMutableArray new];
for (UIView *sub in self.subviews) {
for (UIView *ssub in sub.subviews) {
if ([ssub isKindOfClass:[UIButton class]]) {
UIButton *ssubtn = (UIButton *)ssub;
if (ssubtn.tag == btn.tag) {
btn.selected = !btn.selected;
[self.selseData addObject:[NSString stringWithFormat:@"%ld",btn.tag-250]];
}else{
ssubtn.selected = NO;
}
}
}
}
}
NSLog(@"%@,%ld",self.selseData,self.selseData.count);
}
@end
效果圖如下:
多選:
單選:一樣可以居左、居中、居右
更多代碼demo:https://download.csdn.net/download/qq_33646395/10806449