可以随意更改每行显示的个数,并且自适应占满指定区域。
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@class BackView;
@interface ButtonView : UIView
/** 传入按钮名名字数组 */
@property (nonatomic, strong) NSArray *arrName;
/** 累计的签到数据 */
@property (nonatomic, strong) NSMutableArray *selArr;
/** 是否已签到过 */
@property (nonatomic, assign) BOOL isAdd;
@end
@interface BackView : UIView
@property (nonatomic, strong) UILabel *label;
@property (nonatomic, strong) UILabel *label2;
@property (nonatomic, strong) UIButton *button;
-(void)viewAddViewFromname:(NSString *)name;
@end
NS_ASSUME_NONNULL_END
#import "ButtonView.h"
@interface ButtonView ()
@property (nonatomic, strong) NSMutableArray *arrview;
@end
@implementation ButtonView
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
}
return self;
}
- (void)setArrName:(NSArray *)arrName
{
_arrName = arrName;
_isAdd = NO;
self.arrview = [self buttonFromArr:_arrName count:4 lastBuQi:YES];
}
/**
在指定区域内绘制button组
@param arrName 按钮显示的名字
@param count 单行按钮数量
@param lastBuQi 最后一个是否补齐 也就是让视图范围填满
@return 返回所有的button组
*/
- (NSMutableArray *)buttonFromArr:(NSArray *)arrName count:(NSInteger)count lastBuQi:(BOOL)lastBuQi
{
NSMutableArray *arr = [NSMutableArray array];
if (count <= 0)
{
return arr;
}
// 每行个数
NSInteger yu = arrName.count % count;
// 要显示的行数
NSInteger hang = arrName.count / count;
if ( yu > 0) { hang += 1; }
CGFloat screnWidth = CGRectGetWidth(self.frame);
CGFloat screnheight = CGRectGetHeight(self.frame);
// 顶底间距
CGFloat topBotomSpace = 10;
// 左右间距
CGFloat leftRightSpace = 10;
// 横向间距
CGFloat wSpace = 15;
// 纵向间距
CGFloat hSpace = 10;
CGFloat x = 0;
CGFloat y = topBotomSpace;
CGFloat width = (screnWidth - (count - 1) * wSpace - leftRightSpace * 2) / count;
CGFloat height = (screnheight - topBotomSpace * ( hang- 1 ) - hSpace * 2) / hang;
for (NSInteger i = 0; i < arrName.count; i++)
{
if (x == 0)
{
x += leftRightSpace;
}
else
{
x += wSpace;
}
if (lastBuQi == YES)
{
// 最后一个补齐机制
if (i == arrName.count - 1)
{
if (x + width + leftRightSpace + 1 < screnWidth)
{
width = screnWidth - x - leftRightSpace;
}
}
}
BackView *view = [[BackView alloc]init];
view.frame = CGRectMake(x, y, width, height);
x += width;
// +1 是因为平均分配可能存在小数
if ( x + leftRightSpace + 1 >= screnWidth)
{
x = 0;
y = y + height + hSpace;
}
view.layer.borderWidth = 1;
view.layer.borderColor = [UIColor grayColor].CGColor;
[view viewAddViewFromname:arrName[i]];
[view.button addTarget:self action:@selector(clickSender:) forControlEvents:(UIControlEventTouchUpInside)];
[self addSubview:view];
[arr addObject:view];
}
return arr;
}
/** 点击方法 */
- (void)clickSender:(UIButton *)sender
{
NSInteger index = [self.arrview indexOfObject:sender.superview];
if (_isAdd == NO)
{
if (index == self.selArr.count)
{
[self.selArr addObject:self.arrName[index]];
_isAdd = YES;
}
self.selArr = _selArr;
}
}
/** 刷新 */
- (void)setSelArr:(NSMutableArray *)selArr
{
_selArr = selArr;
for (int i = 0; i< self.arrview.count; i++)
{
BackView *back = self.arrview[i];
if (_selArr.count-1 >= i)
{
back.button.selected = YES;
back.button.userInteractionEnabled = NO;
back.label2.text = @"✨领奖✨";
}
else
{
back.button.selected = NO;
back.button.userInteractionEnabled = YES;
back.label2.text = @"✨";
}
}
}
- (NSMutableArray *)arrview{
if (!_arrview) {
_arrview = [NSMutableArray array];
}
return _arrview;
}
@end
@implementation BackView
-(void)viewAddViewFromname:(NSString *)name
{
UILabel *label = [UILabel new];
label.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height/5 * 3);
label.font = [UIFont systemFontOfSize:18];
label.text = name;
[label setTextAlignment:(NSTextAlignmentCenter)];
[self addSubview:label];
self.label =label;
UILabel *label2 = [UILabel new];
label2.frame = CGRectMake(0, self.frame.size.height/5 * 3, self.frame.size.width, self.frame.size.height/5 * 2);
label2.text = @"领奖";
label2.font = [UIFont systemFontOfSize:17];
label2.layer.cornerRadius = 10;
label2.layer.masksToBounds = YES;
[label2 setTextAlignment:(NSTextAlignmentCenter)];
[self addSubview:label2];
self.label2 = label2;
UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
button.frame = self.bounds;
[button setTitle:@"" forState:(UIControlStateNormal)];
[button setTitle:@"✔" forState:(UIControlStateSelected)];
button.selected = NO;
[self addSubview:button];
self.button = button;
}
@end
// 调用
#import "ViewController.h"
#import "ButtonView.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSArray *arr = @[@"第一天",@"第二天",@"第三天",@"第四天",@"第五天",@"第六天",@"第七天"];
NSArray *selArr = @[@"第一天",@"第二天",@"第三天",@"第四天"];
ButtonView *view = [[ButtonView alloc]initWithFrame:CGRectMake(0, 88, CGRectGetWidth([UIScreen mainScreen].bounds), 200)];
view.arrName = arr;
view.selArr = selArr.mutableCopy;
[self.view addSubview:view];
}
@end