设计可以多选的按钮ChooseManyButton
设计 可以 按钮 多选
2023-09-14 08:57:16 时间
// 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;
@implementation ChooseManyButton
- (void)resetSizeAndCreateButtons {
// 没有元素则退出
if (_titles.count == 0) {
return;
// 初始化标签数组(与标题一一对应)
_chooseTitles = [NSMutableArray new];
for (int i = 0; i _titles.count; i++) {
[_chooseTitles addObject:[NSNumber numberWithBool:NO]];
// 没有设置左边距则默认值为5.f
if (_gapFromLeft == 0) {
_gapFromLeft = 5.f;
// 没有设置水平按钮间距则默认值为5.f
if (_gapFromHorizontalButton == 0) {
_gapFromHorizontalButton = 5.f;
// 没有设置垂直按钮间距则默认值为5.f
if (_gapFromVerticalButton == 0) {
_gapFromVerticalButton = 5.f;
// 没有设置按钮高度则按钮默认高度为20.f
if (_buttonHeight == 0) {
_buttonHeight = 20.f;
// 获取frame宽度
CGFloat frameWidth = self.bounds.size.width;
// 计算出按钮宽度
CGFloat buttonWidth = (frameWidth - _gapFromLeft*2 - _gapFromHorizontalButton)/2.f;
// 动态创建出按钮
for (int i = 0; i _titles.count; i++) {
UIButton *button = [[UIButton alloc] initWithFrame:\
CGRectMake(_gapFromLeft + (buttonWidth + _gapFromHorizontalButton)*(i%2),
(i/2)*(_buttonHeight + _gapFromVerticalButton),
buttonWidth,
_buttonHeight)];
button.tag = FLAG_BUTTON + i;
// 设置按钮圆角
button.layer.cornerRadius = _buttonHeight/2.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 - 1)/2 + 1) + _gapFromVerticalButton*((_titles.count - 1)/2);
CGRect rect = self.frame;
rect.size.height = selfViewHeight;
self.frame = rect;
- (CGRect)calculateFrame {
// 没有元素则退出
if (_titles.count == 0) {
return CGRectZero;
// 没有设置左边距则默认值为5.f
if (_gapFromLeft == 0) {
_gapFromLeft = 5.f;
// 没有设置水平按钮间距则默认值为5.f
if (_gapFromHorizontalButton == 0) {
_gapFromHorizontalButton = 5.f;
// 没有设置垂直按钮间距则默认值为5.f
if (_gapFromVerticalButton == 0) {
_gapFromVerticalButton = 5.f;
// 没有设置按钮高度则按钮默认高度为20.f
if (_buttonHeight == 0) {
_buttonHeight = 20.f;
// 根据控件的一些参数计算出高度
CGFloat selfViewHeight = _buttonHeight*((_titles.count - 1)/2 + 1) + _gapFromVerticalButton*((_titles.count - 1)/2);
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:1]
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:1].CGColor;
button.layer.borderWidth = 1.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:1]
forState:UIControlStateNormal];
if (_selectedButtonBackgroundColor) {
button.backgroundColor = _selectedButtonBackgroundColor;
} else {
button.backgroundColor = [UIColor colorWithRed:0.055 green:0.365 blue:0.663 alpha:1];
button.layer.borderColor = [UIColor colorWithRed:0.055 green:0.365 blue:0.663 alpha:1].CGColor;
button.layer.borderWidth = 1.f;
// 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; @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 50, 320, 30)]; label.textAlignment = NSTextAlignmentCenter; label.text = @"“近代中国睁眼看世界的第一人”的是___"; label.textColor = [UIColor grayColor]; [self.view addSubview:label]; button = [[ChooseManyButton alloc] initWithFrame:CGRectMake(0, 100, 320, 400)]; button.buttonHeight = 25.f; button.gapFromLeft = 10.f; button.gapFromVerticalButton = 20.f; button.gapFromHorizontalButton = 10.f; button.buttonTitleFont = [UIFont systemFontOfSize:16.f]; button.titles = @[@"A. 梁启超", @"B. 龚自珍", @"C. 林则徐", @"D. 李鸿章"]; button.delegate = self; [self.view addSubview:button]; // 设置完所有参数后创建出控件 [button resetSizeAndCreateButtons]; - (void)chooseButtons:(NSArray *)buttons { NSLog(@"%@", buttons); @end
下拉复选框 最近在项目开发中,有的地方用到了下拉复选框,于是再网上找了一下,有很多种写法,但自己感觉不是很好,又不想用插件,因为感觉引入的js太大,功能太繁杂,于是决定自己写一个小demo,效果如下: (改善:当遇到选项比较多时,可以定义一个数组插入到HTML中,实现下拉的选项,可以参考省市区联动的写法:https://www.
// 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; @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 50, 320, 30)]; label.textAlignment = NSTextAlignmentCenter; label.text = @"“近代中国睁眼看世界的第一人”的是___"; label.textColor = [UIColor grayColor]; [self.view addSubview:label]; button = [[ChooseManyButton alloc] initWithFrame:CGRectMake(0, 100, 320, 400)]; button.buttonHeight = 25.f; button.gapFromLeft = 10.f; button.gapFromVerticalButton = 20.f; button.gapFromHorizontalButton = 10.f; button.buttonTitleFont = [UIFont systemFontOfSize:16.f]; button.titles = @[@"A. 梁启超", @"B. 龚自珍", @"C. 林则徐", @"D. 李鸿章"]; button.delegate = self; [self.view addSubview:button]; // 设置完所有参数后创建出控件 [button resetSizeAndCreateButtons]; - (void)chooseButtons:(NSArray *)buttons { NSLog(@"%@", buttons); @end
实时打印信息:
下拉复选框 最近在项目开发中,有的地方用到了下拉复选框,于是再网上找了一下,有很多种写法,但自己感觉不是很好,又不想用插件,因为感觉引入的js太大,功能太繁杂,于是决定自己写一个小demo,效果如下: (改善:当遇到选项比较多时,可以定义一个数组插入到HTML中,实现下拉的选项,可以参考省市区联动的写法:https://www.
相关文章
- 跨平台长连接组件设计及可插拔改造
- 鸿蒙(HarmonyOS)支持低代码开发,无需HTML知识,就可以设计复杂界面
- Android ListView下拉/上拉刷新:设计原理与实现「建议收藏」
- 用html设计一个动漫网站_HTML一个介绍的页面代码
- [答疑]《实现领域驱动设计》的译者其实没错?(二)
- 智药公开课第1期回顾|AI药物设计的前沿思路、进展与未来
- 设计分享|单片机左右来回的流水灯
- 高校学籍管理系统【数据库设计】2020.7.26
- 如何设计 QQ、微信、微博、Github 等第三方账号登陆 ?(附表设计)
- 电影《星际穿越》里的虫洞特效使用 Linux 系统设计
- 微软改进Windows 11 UI设计:修复窗口边框白色像素问题
- 「MySQL数据库设计指南」——25字MySQL数据库设计方案。(mysql数据库设计方案)
- 学习mysql如何设计商品表——实用技巧分享(mysql商品表设计)
- 用ASP设计购物推车