设计可以多选的按钮ChooseManyButton

效果:

源码:

ChooseManyButton.h 与 ChooseManyButton.m

  1. //
  2. // ChooseManyButton.h
  3. // ChooseOnlyButton
  4. //
  5. // Created by YouXianMing on 14/11/5.
  6. // Copyright (c) 2014年 YouXianMing. All rights reserved.
  7. //
  8.  
  9. #import <UIKit/UIKit.h>
  10.  
  11. @protocol ChooseManyButtonDelegate <NSObject>
  12. @optional
  13. - (void)chooseButtons:(NSArray *)buttons;
  14. @end
  15.  
  16. @interface ChooseManyButton : UIButton
  17. /**
  18. * 代理
  19. */
  20. @property (nonatomic, assign) id<ChooseManyButtonDelegate> delegate;
  21.  
  22. /**
  23. * 选择的标题(存储的是NSNumber的BOOL值,与给定的标题值一一对应)
  24. */
  25. @property (nonatomic, strong, readonly) NSMutableArray *chooseTitles;
  26.  
  27. /**
  28. * 标题的数组
  29. */
  30. @property (nonatomic, strong) NSArray *titles;
  31.  
  32. /**
  33. * 按钮离左侧的距离
  34. */
  35. @property (nonatomic, assign) CGFloat gapFromLeft;
  36.  
  37. /**
  38. * 两个按钮之间的水平距离
  39. */
  40. @property (nonatomic, assign) CGFloat gapFromHorizontalButton;
  41.  
  42. /**
  43. * 两个按钮之间的垂直间距
  44. */
  45. @property (nonatomic, assign) CGFloat gapFromVerticalButton;
  46.  
  47. /**
  48. * 按钮高度
  49. */
  50. @property (nonatomic, assign) CGFloat buttonHeight;
  51.  
  52. /**
  53. * 按钮标题字体
  54. */
  55. @property (nonatomic, strong) UIFont *buttonTitleFont;
  56.  
  57. /**
  58. * 没有选中状态下的按钮的背景颜色以及按钮字体的颜色
  59. */
  60. @property (nonatomic, strong) UIColor *normalButtonBackgroundColor;
  61. @property (nonatomic, strong) UIColor *normalButtonTitleColor;
  62.  
  63. /**
  64. * 选中状态下的按钮的背景颜色以及按钮字体的颜色
  65. */
  66. @property (nonatomic, strong) UIColor *selectedButtonBackgroundColor;
  67. @property (nonatomic, strong) UIColor *selectedButtonTitleColor;
  68.  
  69. /**
  70. * 重设view的尺寸并且创建出新的按钮
  71. */
  72. - (void)resetSizeAndCreateButtons;
  73.  
  74. /**
  75. * 重新计算frame
  76. *
  77. * @return frame值
  78. */
  79. - (CGRect)calculateFrame;
  80.  
  81. @end
  1. //
  2. // ChooseManyButton.m
  3. // ChooseOnlyButton
  4. //
  5. // Created by YouXianMing on 14/11/5.
  6. // Copyright (c) 2014年 YouXianMing. All rights reserved.
  7. //
  8.  
  9. #import "ChooseManyButton.h"
  10.  
  11. typedef enum : NSUInteger {
  12. FLAG_BUTTON = 0x2244,
  13. } ENUM_FLAG;
  14.  
  15. @interface ChooseManyButton ()
  16.  
  17. /**
  18. * 标签数组
  19. */
  20. @property (nonatomic, strong) NSMutableArray *chooseTitles;
  21.  
  22. @end
  23.  
  24. @implementation ChooseManyButton
  25.  
  26. - (void)resetSizeAndCreateButtons {
  27.  
  28. // 没有元素则退出
  29. if (_titles.count == ) {
  30. return;
  31. }
  32.  
  33. // 初始化标签数组(与标题一一对应)
  34. _chooseTitles = [NSMutableArray new];
  35. for (int i = ; i < _titles.count; i++) {
  36. [_chooseTitles addObject:[NSNumber numberWithBool:NO]];
  37. }
  38.  
  39. // 没有设置左边距则默认值为5.f
  40. if (_gapFromLeft == ) {
  41. _gapFromLeft = .f;
  42. }
  43.  
  44. // 没有设置水平按钮间距则默认值为5.f
  45. if (_gapFromHorizontalButton == ) {
  46. _gapFromHorizontalButton = .f;
  47. }
  48.  
  49. // 没有设置垂直按钮间距则默认值为5.f
  50. if (_gapFromVerticalButton == ) {
  51. _gapFromVerticalButton = .f;
  52. }
  53.  
  54. // 没有设置按钮高度则按钮默认高度为20.f
  55. if (_buttonHeight == ) {
  56. _buttonHeight = .f;
  57. }
  58.  
  59. // 获取frame宽度
  60. CGFloat frameWidth = self.bounds.size.width;
  61.  
  62. // 计算出按钮宽度
  63. CGFloat buttonWidth = (frameWidth - _gapFromLeft* - _gapFromHorizontalButton)/.f;
  64.  
  65. // 动态创建出按钮
  66. for (int i = ; i < _titles.count; i++) {
  67. UIButton *button = [[UIButton alloc] initWithFrame:\
  68. CGRectMake(_gapFromLeft + (buttonWidth + _gapFromHorizontalButton)*(i%),
  69. (i/)*(_buttonHeight + _gapFromVerticalButton),
  70. buttonWidth,
  71. _buttonHeight)];
  72. button.tag = FLAG_BUTTON + i;
  73.  
  74. // 设置按钮圆角
  75. button.layer.cornerRadius = _buttonHeight/.f;
  76. [button addTarget:self
  77. action:@selector(buttonsEvent:)
  78. forControlEvents:UIControlEventTouchUpInside];
  79.  
  80. // 设置按钮标题 + 默认的标题颜色
  81. [button setTitle:_titles[i] forState:UIControlStateNormal];
  82. [self normalButtonStyle:button];
  83.  
  84. // 设置字体
  85. if (_buttonTitleFont) {
  86. button.titleLabel.font = _buttonTitleFont;
  87. }
  88.  
  89. [self addSubview:button];
  90. }
  91.  
  92. // 重设自身view高度
  93. CGFloat selfViewHeight = _buttonHeight*((_titles.count - )/ + ) + _gapFromVerticalButton*((_titles.count - )/);
  94. CGRect rect = self.frame;
  95. rect.size.height = selfViewHeight;
  96. self.frame = rect;
  97. }
  98.  
  99. - (CGRect)calculateFrame {
  100.  
  101. // 没有元素则退出
  102. if (_titles.count == ) {
  103. return CGRectZero;
  104. }
  105.  
  106. // 没有设置左边距则默认值为5.f
  107. if (_gapFromLeft == ) {
  108. _gapFromLeft = .f;
  109. }
  110.  
  111. // 没有设置水平按钮间距则默认值为5.f
  112. if (_gapFromHorizontalButton == ) {
  113. _gapFromHorizontalButton = .f;
  114. }
  115.  
  116. // 没有设置垂直按钮间距则默认值为5.f
  117. if (_gapFromVerticalButton == ) {
  118. _gapFromVerticalButton = .f;
  119. }
  120.  
  121. // 没有设置按钮高度则按钮默认高度为20.f
  122. if (_buttonHeight == ) {
  123. _buttonHeight = .f;
  124. }
  125.  
  126. // 根据控件的一些参数计算出高度
  127. CGFloat selfViewHeight = _buttonHeight*((_titles.count - )/ + ) + _gapFromVerticalButton*((_titles.count - )/);
  128. CGRect rect = self.frame;
  129. rect.size.height = selfViewHeight;
  130.  
  131. // 返回控件
  132. return rect;
  133. }
  134.  
  135. - (void)buttonsEvent:(UIButton *)button {
  136.  
  137. // 获取到点击按钮的index值
  138. NSInteger index = button.tag - FLAG_BUTTON;
  139.  
  140. // 根据值来确定设置样式
  141. if ([_chooseTitles[index] isEqual: @NO]) {
  142. _chooseTitles[index] = @YES;
  143. [self selectButtonStyle:button];
  144. } else {
  145. _chooseTitles[index] = @NO;
  146. [self normalButtonStyle:button];
  147. }
  148.  
  149. // 通过代理获取点击了哪些按钮
  150. if (_delegate && [_delegate respondsToSelector:@selector(chooseButtons:)]) {
  151. [_delegate chooseButtons:_chooseTitles];
  152. }
  153. }
  154.  
  155. #pragma mark - 私有方法
  156. /**
  157. * 普通按钮的样式
  158. *
  159. * @param button 要改变样式的按钮
  160. */
  161. - (void)normalButtonStyle:(UIButton *)button {
  162.  
  163. if (_normalButtonTitleColor) {
  164. [button setTitleColor:_normalButtonTitleColor
  165. forState:UIControlStateNormal];
  166. } else {
  167. [button setTitleColor:[UIColor colorWithRed:0.000 green:0.361 blue:0.671 alpha:]
  168. forState:UIControlStateNormal];
  169. }
  170.  
  171. if (_normalButtonBackgroundColor) {
  172. button.backgroundColor = _normalButtonBackgroundColor;
  173. } else {
  174. button.backgroundColor = [UIColor clearColor];
  175. }
  176.  
  177. button.layer.borderColor = [UIColor colorWithRed:0.843 green:0.843 blue:0.843 alpha:].CGColor;
  178. button.layer.borderWidth = .f;
  179. }
  180.  
  181. /**
  182. * 选中按钮时的样式
  183. *
  184. * @param button 要改变样式的按钮
  185. */
  186. - (void)selectButtonStyle:(UIButton *)button {
  187.  
  188. if (_selectedButtonTitleColor) {
  189. [button setTitleColor:_selectedButtonTitleColor
  190. forState:UIControlStateNormal];
  191. } else {
  192. [button setTitleColor:[UIColor colorWithRed:0.973 green:0.984 blue:0.988 alpha:]
  193. forState:UIControlStateNormal];
  194. }
  195.  
  196. if (_selectedButtonBackgroundColor) {
  197. button.backgroundColor = _selectedButtonBackgroundColor;
  198. } else {
  199. button.backgroundColor = [UIColor colorWithRed:0.055 green:0.365 blue:0.663 alpha:];
  200. }
  201.  
  202. button.layer.borderColor = [UIColor colorWithRed:0.055 green:0.365 blue:0.663 alpha:].CGColor;
  203. button.layer.borderWidth = .f;
  204. }
  205.  
  206. @end

使用时的代码:

  1. //
  2. // ViewController.m
  3. // ChooseOnlyButton
  4. //
  5. // Created by YouXianMing on 14/11/4.
  6. // Copyright (c) 2014年 YouXianMing. All rights reserved.
  7. //
  8.  
  9. #import "ViewController.h"
  10. #import "ChooseManyButton.h"
  11.  
  12. @interface ViewController ()<ChooseManyButtonDelegate>
  13.  
  14. {
  15. ChooseManyButton *button;
  16. }
  17.  
  18. @end
  19.  
  20. @implementation ViewController
  21.  
  22. - (void)viewDidLoad {
  23. [super viewDidLoad];
  24.  
  25. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(, , , )];
  26. label.textAlignment = NSTextAlignmentCenter;
  27. label.text = @"“近代中国睁眼看世界的第一人”的是___";
  28. label.textColor = [UIColor grayColor];
  29. [self.view addSubview:label];
  30.  
  31. button = [[ChooseManyButton alloc] initWithFrame:CGRectMake(, , , )];
  32. button.buttonHeight = .f;
  33. button.gapFromLeft = .f;
  34. button.gapFromVerticalButton = .f;
  35. button.gapFromHorizontalButton = .f;
  36. button.buttonTitleFont = [UIFont systemFontOfSize:.f];
  37. button.titles = @[@"A. 梁启超",
  38. @"B. 龚自珍",
  39. @"C. 林则徐",
  40. @"D. 李鸿章"];
  41. button.delegate = self;
  42. [self.view addSubview:button];
  43.  
  44. // 设置完所有参数后创建出控件
  45. [button resetSizeAndCreateButtons];
  46. }
  47.  
  48. - (void)chooseButtons:(NSArray *)buttons {
  49. NSLog(@"%@", buttons);
  50. }
  51.  
  52. @end

实时打印信息:

设计可以多选的按钮ChooseManyButton的更多相关文章

  1. 设计多选一按钮ChooseOnlyButton

    设计多选一按钮ChooseOnlyButton 效果: 源码: ChooseOnlyButton.h 与 ChooseOnlyButton.m // // ChooseOnlyButton.h // ...

  2. 定制二选一按钮SwitchButton

    定制二选一按钮SwitchButton 效果: 源码: SwitchButton.h 与 SwitchButton.m // // SwitchButton.h // KongJian // // C ...

  3. 界面设计中如何增强CTA按钮召唤力?

    以下内容由Mockplus(摹客)团队翻译整理,仅供学习交流,Mockplus是更快更简单的原型设计工具. 网页和软件应用之类数字产品的有效交互系统一般是由拥有各种任务和功能的小元素构成.而为创建更加 ...

  4. cocos2dx 3.x (单选,多选,复选checkBox按钮的实现) RadioButton

    // //  LandLordsMakeNewRoom.hpp //  MalaGame39 // //  Created by work on 2016/12/19. // //   #ifndef ...

  5. JS实现简单的多选选项的全选反选按钮

    1 <!DOCTYPE html> 2 <html> 3 <head lang="en"> 4 <!-- 5 需求: 6 1.写三个按钮: ...

  6. js全选反选按钮实现

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  7. 用DotNetBar设计的 Gradient Buttons 漂亮按钮

       http://www.webdesignerwall.com/demo/css-buttons.html public class GradientButtons : DevComponents ...

  8. listview禁止双击一条之后选中复选框按钮的方法

    this.listViewUsers.SelectedItems[0].Checked = !this.listViewUsers.SelectedItems[0].Checked;

  9. openerp学习笔记 tree视图增加复选处理按钮

    wizard:用于确认或选择 wizard/sale_multi_action.py # -*- encoding: utf-8 -*-from openerp.osv import fields, ...

随机推荐

  1. Java 生成指定范围的随机数

    /** * 生成[min, max]之间的随机整数 * * @param min 最小整数 * @param max 最大整数 * @return * @author jqlin */ private ...

  2. glide 解决 golang.org/x/net 等依赖包无法获取

    知道glide有设置镜像功能,可以把某个依赖包的源地址切换为另一个地址,相当于切换到镜像地址,用于某些依赖包被墙的原因 之前碰到 golang.org/x/net,设置镜像: glide mirror ...

  3. Linux下安装了Xampp,但命令行使用不了MySQL

    引用:http://www.2cto.com/database/201406/309521.html   刚安装好Xampp,使用localhost也能正常运行, phpMyAdimin也能正常登录 ...

  4. 深度剖析Dubbo源码

    -----------------学习dubbo源码,能给你带来什么好处?----------- 1.提升SOA的微服务架构设计能力   通过读dubbo源码是一条非常不错的通往SOA架构设计之路,毕 ...

  5. Linux 文件流管理

    1. 打开/关闭文件 1). 打开文件 / fopen 作用: 打开一个文件,将其与文件流联系起来,方便后续的操作 头文件: #include <stdio.h> 函数原型: FILE * ...

  6. Ionic APP 热更新 之 产品发布状态下的热更新搭建,去local-dev-addon插件

    上一篇,我们介绍了在本地开发环境下的ionic项目热更新测试, 本文,我们将详细说明如何在去掉cordova-hot-code-push-local-dev-addon插件的情况下,实现热更新. 使用 ...

  7. c#基础学习(0703)之string.Format格式化日期

    C# string.Format格式化日期 DateTime dt = ,,,,,,); string.Format("{0:y yy yyy yyyy}",dt); //17 1 ...

  8. 设计模式学习--面向对象的5条设计原则之接口隔离原则--ISP

    一.ISP简介(ISP--Interface Segregation Principle): 使用多个专门的接口比使用单一的总接口要好.一个类对另外一个类的依赖性应当是建立在最小的接口上的.一个接口代 ...

  9. WPF进度条

    ProgressBar控件与传统WinForm使用方法完全一样,我们只需关注: Minimum——最小值,默认为0: Maximum——最大值,默认为100. Value——当前值.   关键是它的控 ...

  10. day-01mysql数据库下载安装卸载及基本操作

    MySQL5.5.40破解版地址(永久有效):链接:https://pan.baidu.com/s/1n-sODjoCdeSGP8bDGxl23Q 密码:qjjy 第2节 数据库的介绍 MySQL:开 ...