设计可以多选的按钮ChooseManyButton

效果:

源码:

ChooseManyButton.h 与 ChooseManyButton.m

//
// ChooseManyButton.h
// ChooseOnlyButton
//
// Created by YouXianMing on 14/11/5.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import <UIKit/UIKit.h> @protocol ChooseManyButtonDelegate <NSObject>
@optional
- (void)chooseButtons:(NSArray *)buttons;
@end @interface ChooseManyButton : UIButton
/**
* 代理
*/
@property (nonatomic, assign) id<ChooseManyButtonDelegate> delegate; /**
* 选择的标题(存储的是NSNumber的BOOL值,与给定的标题值一一对应)
*/
@property (nonatomic, strong, readonly) NSMutableArray *chooseTitles; /**
* 标题的数组
*/
@property (nonatomic, strong) NSArray *titles; /**
* 按钮离左侧的距离
*/
@property (nonatomic, assign) CGFloat gapFromLeft; /**
* 两个按钮之间的水平距离
*/
@property (nonatomic, assign) CGFloat gapFromHorizontalButton; /**
* 两个按钮之间的垂直间距
*/
@property (nonatomic, assign) CGFloat gapFromVerticalButton; /**
* 按钮高度
*/
@property (nonatomic, assign) CGFloat buttonHeight; /**
* 按钮标题字体
*/
@property (nonatomic, strong) UIFont *buttonTitleFont; /**
* 没有选中状态下的按钮的背景颜色以及按钮字体的颜色
*/
@property (nonatomic, strong) UIColor *normalButtonBackgroundColor;
@property (nonatomic, strong) UIColor *normalButtonTitleColor; /**
* 选中状态下的按钮的背景颜色以及按钮字体的颜色
*/
@property (nonatomic, strong) UIColor *selectedButtonBackgroundColor;
@property (nonatomic, strong) UIColor *selectedButtonTitleColor; /**
* 重设view的尺寸并且创建出新的按钮
*/
- (void)resetSizeAndCreateButtons; /**
* 重新计算frame
*
* @return frame值
*/
- (CGRect)calculateFrame; @end
//
// ChooseManyButton.m
// ChooseOnlyButton
//
// Created by YouXianMing on 14/11/5.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import "ChooseManyButton.h" typedef enum : NSUInteger {
FLAG_BUTTON = 0x2244,
} ENUM_FLAG; @interface ChooseManyButton () /**
* 标签数组
*/
@property (nonatomic, strong) NSMutableArray *chooseTitles; @end @implementation ChooseManyButton - (void)resetSizeAndCreateButtons { // 没有元素则退出
if (_titles.count == ) {
return;
} // 初始化标签数组(与标题一一对应)
_chooseTitles = [NSMutableArray new];
for (int i = ; i < _titles.count; i++) {
[_chooseTitles addObject:[NSNumber numberWithBool:NO]];
} // 没有设置左边距则默认值为5.f
if (_gapFromLeft == ) {
_gapFromLeft = .f;
} // 没有设置水平按钮间距则默认值为5.f
if (_gapFromHorizontalButton == ) {
_gapFromHorizontalButton = .f;
} // 没有设置垂直按钮间距则默认值为5.f
if (_gapFromVerticalButton == ) {
_gapFromVerticalButton = .f;
} // 没有设置按钮高度则按钮默认高度为20.f
if (_buttonHeight == ) {
_buttonHeight = .f;
} // 获取frame宽度
CGFloat frameWidth = self.bounds.size.width; // 计算出按钮宽度
CGFloat buttonWidth = (frameWidth - _gapFromLeft* - _gapFromHorizontalButton)/.f; // 动态创建出按钮
for (int i = ; i < _titles.count; i++) {
UIButton *button = [[UIButton alloc] initWithFrame:\
CGRectMake(_gapFromLeft + (buttonWidth + _gapFromHorizontalButton)*(i%),
(i/)*(_buttonHeight + _gapFromVerticalButton),
buttonWidth,
_buttonHeight)];
button.tag = FLAG_BUTTON + i; // 设置按钮圆角
button.layer.cornerRadius = _buttonHeight/.f;
[button addTarget:self
action:@selector(buttonsEvent:)
forControlEvents:UIControlEventTouchUpInside]; // 设置按钮标题 + 默认的标题颜色
[button setTitle:_titles[i] forState:UIControlStateNormal];
[self normalButtonStyle:button]; // 设置字体
if (_buttonTitleFont) {
button.titleLabel.font = _buttonTitleFont;
} [self addSubview:button];
} // 重设自身view高度
CGFloat selfViewHeight = _buttonHeight*((_titles.count - )/ + ) + _gapFromVerticalButton*((_titles.count - )/);
CGRect rect = self.frame;
rect.size.height = selfViewHeight;
self.frame = rect;
} - (CGRect)calculateFrame { // 没有元素则退出
if (_titles.count == ) {
return CGRectZero;
} // 没有设置左边距则默认值为5.f
if (_gapFromLeft == ) {
_gapFromLeft = .f;
} // 没有设置水平按钮间距则默认值为5.f
if (_gapFromHorizontalButton == ) {
_gapFromHorizontalButton = .f;
} // 没有设置垂直按钮间距则默认值为5.f
if (_gapFromVerticalButton == ) {
_gapFromVerticalButton = .f;
} // 没有设置按钮高度则按钮默认高度为20.f
if (_buttonHeight == ) {
_buttonHeight = .f;
} // 根据控件的一些参数计算出高度
CGFloat selfViewHeight = _buttonHeight*((_titles.count - )/ + ) + _gapFromVerticalButton*((_titles.count - )/);
CGRect rect = self.frame;
rect.size.height = selfViewHeight; // 返回控件
return rect;
} - (void)buttonsEvent:(UIButton *)button { // 获取到点击按钮的index值
NSInteger index = button.tag - FLAG_BUTTON; // 根据值来确定设置样式
if ([_chooseTitles[index] isEqual: @NO]) {
_chooseTitles[index] = @YES;
[self selectButtonStyle:button];
} else {
_chooseTitles[index] = @NO;
[self normalButtonStyle:button];
} // 通过代理获取点击了哪些按钮
if (_delegate && [_delegate respondsToSelector:@selector(chooseButtons:)]) {
[_delegate chooseButtons:_chooseTitles];
}
} #pragma mark - 私有方法
/**
* 普通按钮的样式
*
* @param button 要改变样式的按钮
*/
- (void)normalButtonStyle:(UIButton *)button { if (_normalButtonTitleColor) {
[button setTitleColor:_normalButtonTitleColor
forState:UIControlStateNormal];
} else {
[button setTitleColor:[UIColor colorWithRed:0.000 green:0.361 blue:0.671 alpha:]
forState:UIControlStateNormal];
} if (_normalButtonBackgroundColor) {
button.backgroundColor = _normalButtonBackgroundColor;
} else {
button.backgroundColor = [UIColor clearColor];
} button.layer.borderColor = [UIColor colorWithRed:0.843 green:0.843 blue:0.843 alpha:].CGColor;
button.layer.borderWidth = .f;
} /**
* 选中按钮时的样式
*
* @param button 要改变样式的按钮
*/
- (void)selectButtonStyle:(UIButton *)button { if (_selectedButtonTitleColor) {
[button setTitleColor:_selectedButtonTitleColor
forState:UIControlStateNormal];
} else {
[button setTitleColor:[UIColor colorWithRed:0.973 green:0.984 blue:0.988 alpha:]
forState:UIControlStateNormal];
} if (_selectedButtonBackgroundColor) {
button.backgroundColor = _selectedButtonBackgroundColor;
} else {
button.backgroundColor = [UIColor colorWithRed:0.055 green:0.365 blue:0.663 alpha:];
} button.layer.borderColor = [UIColor colorWithRed:0.055 green:0.365 blue:0.663 alpha:].CGColor;
button.layer.borderWidth = .f;
} @end

使用时的代码:

//
// ViewController.m
// ChooseOnlyButton
//
// Created by YouXianMing on 14/11/4.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import "ViewController.h"
#import "ChooseManyButton.h" @interface ViewController ()<ChooseManyButtonDelegate> {
ChooseManyButton *button;
} @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(, , , )];
label.textAlignment = NSTextAlignmentCenter;
label.text = @"“近代中国睁眼看世界的第一人”的是___";
label.textColor = [UIColor grayColor];
[self.view addSubview:label]; button = [[ChooseManyButton alloc] initWithFrame:CGRectMake(, , , )];
button.buttonHeight = .f;
button.gapFromLeft = .f;
button.gapFromVerticalButton = .f;
button.gapFromHorizontalButton = .f;
button.buttonTitleFont = [UIFont systemFontOfSize:.f];
button.titles = @[@"A. 梁启超",
@"B. 龚自珍",
@"C. 林则徐",
@"D. 李鸿章"];
button.delegate = self;
[self.view addSubview:button]; // 设置完所有参数后创建出控件
[button resetSizeAndCreateButtons];
} - (void)chooseButtons:(NSArray *)buttons {
NSLog(@"%@", buttons);
} @end

实时打印信息:

最新文章

  1. Halcon与MFC交互编程
  2. poj 2711 Leapin&#39; Lizards &amp;&amp; BZOJ 1066: [SCOI2007]蜥蜴 最大流
  3. HTTP的报文格式,GET和POST的区别
  4. OCP-1Z0-051-题目解析-第6题
  5. 1054: [HAOI2008]移动玩具
  6. .Neter玩转Linux系列之五:crontab使用详解和Linux的进程管理以及网络状态监控
  7. JavaScript--我发现,原来你是这样的JS(四)(看看变量,作用域,垃圾回收机制是啥)
  8. serializeArray()获取的表单参数转化成json格式的对象
  9. 如果去掉UITableView上的section的headerView和footerView的悬浮效果
  10. 从壹开始微服务 [ DDD ] 之九 ║从军事故事中,明白领域命令验证(上)
  11. C# 实现对PPT文档加密、解密以及重置密码的操作
  12. WPF 将数据源绑定到TreeView控件出现界面卡死的情况
  13. consul服务发现和配置共享的软件,
  14. PHPEmailer使用简介(以qq邮箱为例)
  15. 创建新的Cocos2dx 3.0项目并解决一些编译问题
  16. 进程枚举之PSAPI函数
  17. timer Compliant Controller project (1)--Product introduction meeting
  18. iis 在站点中新建虚拟目录站点之后,虚拟目录中的 web.config 与 主站点中的 web.config冲突解决方案
  19. django基础2
  20. HDU4405 Aeroplane chess 飞行棋 期望dp 简单

热门文章

  1. 使用subgit进行svn迁移至git(branch,tags)
  2. Python 日期时间处理模块学习笔记
  3. Linux-(diff)
  4. 【LeetCode题解】206_反转链表(Reverse-Linked-List)
  5. google tensorflow bert代码分析
  6. [转]iCheck表单美化插件使用方法详解(含参数、事件等)
  7. [转]How to Import a Text File into SQL Server 2012
  8. js,需要更多源字符
  9. jsonp跨域简单应用(一)
  10. commons-fileupload-1.4使用及问题