设计多选一按钮ChooseOnlyButton

news/2024/5/20 23:22:23/文章来源:https://blog.csdn.net/weixin_30690833/article/details/98237054

设计多选一按钮ChooseOnlyButton

效果:

源码:

ChooseOnlyButton.h 与 ChooseOnlyButton.m

//
//  ChooseOnlyButton.h
//  ChooseOnlyButton
//
//  Created by YouXianMing on 14/11/4.
//  Copyright (c) 2014年 YouXianMing. All rights reserved.
//

#import <UIKit/UIKit.h>
@class ChooseOnlyButton;@protocol ChooseOnlyButtonDelegate <NSObject>
@optional
- (void)chooseButtonTitle:(NSString *)title;
@end@interface ChooseOnlyButton : UIView/***  代理*/
@property (nonatomic, assign) id<ChooseOnlyButtonDelegate>  delegate;/***  选取的按钮的标题(只读)*/
@property (nonatomic, strong, readonly)  NSString *selectedTitle;/***  标题的数组*/
@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
//
//  ChooseOnlyButton.m
//  ChooseOnlyButton
//
//  Created by YouXianMing on 14/11/4.
//  Copyright (c) 2014年 YouXianMing. All rights reserved.
//

#import "ChooseOnlyButton.h"@implementation ChooseOnlyButton- (instancetype)initWithFrame:(CGRect)frame
{self = [super initWithFrame:frame];if (self) {}return self;
}- (void)resetSizeAndCreateButtons {// 没有元素则退出if (_titles.count == 0) {return;}// 没有设置左边距则默认值为5.fif (_gapFromLeft == 0) {_gapFromLeft = 5.f;}// 没有设置水平按钮间距则默认值为5.fif (_gapFromHorizontalButton == 0) {_gapFromHorizontalButton = 5.f;}// 没有设置垂直按钮间距则默认值为5.fif (_gapFromVerticalButton == 0) {_gapFromVerticalButton = 5.f;}// 没有设置按钮高度则按钮默认高度为20.fif (_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.layer.cornerRadius = _buttonHeight/2.f;[button addTarget:selfaction:@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.fif (_gapFromLeft == 0) {_gapFromLeft = 5.f;}// 没有设置水平按钮间距则默认值为5.fif (_gapFromHorizontalButton == 0) {_gapFromHorizontalButton = 5.f;}// 没有设置垂直按钮间距则默认值为5.fif (_gapFromVerticalButton == 0) {_gapFromVerticalButton = 5.f;}// 没有设置按钮高度则按钮默认高度为20.fif (_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 {[[self subviews] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {if ([obj isKindOfClass:[UIButton class]]) {if ([button isEqual:obj]) {// 选中按钮的样式
                [self selectButtonStyle:obj];UIButton *button = (UIButton *)obj;// 获取到选取的按钮标题_selectedTitle = button.titleLabel.text;// 代理if (_delegate && [_delegate respondsToSelector:@selector(chooseButtonTitle:)]) {[_delegate chooseButtonTitle:button.titleLabel.text];}} else {[self normalButtonStyle:obj];}}}];
}#pragma mark - 私有方法
/***  普通按钮的样式**  @param button 要改变样式的按钮*/
- (void)normalButtonStyle:(UIButton *)button {if (_normalButtonTitleColor) {[button setTitleColor:_normalButtonTitleColorforState: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:_selectedButtonTitleColorforState: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;
}@end

使用时候的源码:

//
//  ViewController.m
//  ChooseOnlyButton
//
//  Created by YouXianMing on 14/11/4.
//  Copyright (c) 2014年 YouXianMing. All rights reserved.
//

#import "ViewController.h"
#import "ChooseOnlyButton.h"@interface ViewController ()<ChooseOnlyButtonDelegate>{ChooseOnlyButton *button;
}@end@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                         = [[ChooseOnlyButton 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. blueShit",@"C. YouXianMing",@"D. 不知道"];button.delegate                = self;[self.view addSubview:button];// 设置完所有参数后创建出控件
    [button resetSizeAndCreateButtons];
}#pragma mark - 代理
- (void)chooseButtonTitle:(NSString *)title {NSLog(@"%@", title);NSLog(@"%@", button.selectedTitle);
}@end

以下是需要注意的地方:

超高的可定制性

 

转载于:https://www.cnblogs.com/YouXianMing/p/4073513.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.luyixian.cn/news_show_804997.aspx

如若内容造成侵权/违法违规/事实不符,请联系dt猫网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

10个提供免费PHP脚本下载的网站

本文将重点介绍10个PHP脚本的免费资源下载站。之前推荐 《16个下载超酷脚本的热门网站》&#xff0c;这些网站除了PHP脚本&#xff0c;还有JavaScript、Java、Perl、ASP等脚本。如果你已是脚本代码巧匠&#xff0c;不妨把你的出色脚本放到Code Canyon网站上去出售&#xff0c;这…

一些网站的图片只要修改 URL 地址就能得到任何尺寸的图片

2019独角兽企业重金招聘Python工程师标准>>> 时光网: http://img21.mtime.cn/CMS/Gallery/2011/07/02/170837.97014036_160X160.jpg http://img21.mtime.cn/CMS/Gallery/2011/07/02/170837.97014036_362X571.jpg http://img21.mtime.cn/CMS/Gallery/2011/07/02/1708…

Web服务器安全服务,服务器防攻击,服务器防木马,服务器权限配置-网站安全服务...

2019独角兽企业重金招聘Python工程师标准>>> 网站安全服务 QQ: 联系 285691605 网站地址 http://blog.bypat.com Bypat 安全维护服务针对与网站被挂马&#xff0c;网站被挂黑链&#xff0c;首页被篡改&#xff0c;网站被黑客入侵&#xff0c;网站被攻击&#xff0c…

iis7.0上发布mvc4.0网站

步骤如下&#xff1a; 1.右击需要发布的项目&#xff0c;在弹出的菜单中选择“发布...”选项 2.在“发布web”对话框中进行设置&#xff0c;配置文件名称默认为“配置文件1”可以修改为需要的名字&#xff0c;以便识别&#xff0c;也可以不改。发布方法选择“文件系统”&#x…

新手学习在Ubuntu 14.04搭建Javaweb网站(2)--开启SSH服务

2019独角兽企业重金招聘Python工程师标准>>> SSH分客户端openssh-client和openssh-server 我们这里要安装的openssh-server 直接输入命令&#xff1a;sudo apt-get install openssh-server 然后确认sshserver是否启动了&#xff1a;ps -e |grep ssh如果看到sshd那…

从12306网站新验证码看Web验证码设计与破解

2019独角兽企业重金招聘Python工程师标准>>> 铁路官方购票网站12306又出新招&#xff0c;在登录界面推出了全新的验证方式&#xff0c;用户在填写好登录名和密码之后&#xff0c;还要准确的选取图片验证码才能登陆成功。据悉&#xff0c;12306验证码改版后&#xff…

网站服务端开发

2019独角兽企业重金招聘Python工程师标准>>> Memcached vs. Redis?ASP.NET Session State Management With Redis (Local Server Farm Testing) 转载于:https://my.oschina.net/ITELITE/blog/515708

python unittest库 官方网站

为什么80%的码农都做不了架构师&#xff1f;>>> 摘要里面是python unittest的官方摘要&#xff0c;如果有不懂的可以去查。 下面是一个老外写的一个博客也不错&#xff0c;可以看看&#xff1a; http://pyunit.sourceforge.net/pyunit.html 转载于:https://my.o…

亲历钓鱼网站

几个环节都让人无暇思考其真实性&#xff0c;直到我刚好去查看了邮箱帐号&#xff0c;发现根本没有这个帐号&#xff0c;才反应过来。

艾宾浩斯计划表自动生成网站_恋练有词高频词组整理笔记三(附艾宾浩斯计划表自动生成小技巧)...

点击关注不错过每一份精彩笔记&#xff01;温馨提示回复【考研福利】获取考研资料回复【VIP】查看2020考研服务包2019-08-21考研倒计时&#xff1a;122天导LEAD语今天四六级可以查分了&#xff0c;参加了考试的小伙伴可以怀着激动又刺激的心情打开查分界面了。再次表明一下&…

在网站中引入特殊字体

我的天~~这设计稿真好看&#xff01;又有给自己加戏写效果的冲动咯。。。 这都是些什么字体&#xff1f;&#xff1f;说好的微软雅黑法大宋体呢。。 又是一场切图的恶战。。。这小图标这么多&#xff0c;切个雪碧图还要做分辨率、兼容性适应。。不划算啊。。 所以这种时候就是特…

MiniARM工控核心板之-电平转换电路分析(下)(转载于周立功网站)

转载于&#xff1a;http://www.zlg.cn/ipc/article/detail/id/521.html晶体管上拉电阻通过双极性晶体管&#xff0c;集电极由上拉电阻接到电源&#xff0c;输入的高电平的电压值就是电源电压值。以MiniARM核心板与GPRS模块为例&#xff0c;如图 1所示&#xff1a; 图1 晶体管电…

MVC5 网站开发之七 用户功能 2 用户添加和浏览

目录 MVC5网站开发之一 总体概述 MVC5 网站开发之二 创建项目 MVC5 网站开发之三 数据存储层功能实现 MVC5 网站开发之四 业务逻辑层的架构和基本功能 MVC5 网站开发之五 展示层架构 MVC5 网站开发之六 管理员 1、登录、验证和注销 MVC5 网站开发之六 管理员 2、添加、删除、重…

大型网站架构演进(8)业务拆分

大型网站为了应对日益复杂的业务需求&#xff0c;通过使用分而治之的手段将整个网站的业务分成不同的产品线&#xff0c;然后交给不同的开发团队负责。这样一方面方便应用的扩展和维护&#xff0c;同时不同的应用对应不同的数据库&#xff0c;也减小了原来所有业务数据都在一个…

乐光云服务器网站,乐光云服务器地址

乐光云服务器地址 内容精选换一换本节操作介绍通过华为云APP连接Linux实例的操作步骤。云服务器状态为“运行中”。已获取Linux云服务器用户名和密码&#xff0c;忘记密码请参考在控制台重置云耀云服务器密码重置密码。云耀云服务器已经绑定弹性公网IP。所在安全组入方向已开放…

【转】用node.js爬取网站图片并保存

2019独角兽企业重金招聘Python工程师标准>>> 原文&#xff1a;http://www.jianshu.com/p/177ca8aaf6fb 昨天是传说中的程序员节&#xff0c;虽然我对于这个并无感&#xff0c;但还是来搞点事吧&#xff0c;写一个最简单的爬虫&#xff0c;抓取图片并保存在本地&…

成人网站性能提升20倍之经验谈

色情业是个大行业。互联网上没有多少网站的流量能和最大的色情网站相匹敌。 要搞定这巨大的流量很难。更困难的是&#xff0c;在色情网站上提供的很多内容都是低延迟的实时流媒体而不是简单的静态视频。但是对于所有碰到过的挑战&#xff0c;我很少看到有搞定过它们的开发人员写…

Robot Framework 学习(1)- 简单网站兼容性测试

Robot Framework 简单网站兼容性测试0.Robot Framework 简介 Robot Framework 是一个通用的自动化测试框架&#xff0c;主要用于“验收测试”和“验收测试驱动开发(ATDD)” &#xff08;会其它文章中会详细介绍ATDD&#xff09;。它使用的是表格式的测试数据语法&#xff0c;并…

国内五款好用的开源建站系统

国内的开源建站产品已经占据了建站系统的半壁江山&#xff0c;由于二次开发成本低&#xff0c;越来越多的建站者也倾向于选择开源产品来建站。今天推荐5款优秀的开源建站系统&#xff0c;都有免费版本&#xff0c;有需要可以去试试。 ECTouch ECTouch是一款开源免费的移动商城网…

阿里云域名解析+网站备案

1.登录阿里云服务器后&#xff1a; 域名解析&#xff1a;域名与网站&#xff08;万网&#xff09;》域名》解析》修改&#xff08;修改ip即可,这里修改的ip是你要解析指向的&#xff09; 2.网站备案&#xff1a;&#xff08;某一天的早上照常上班&#xff0c;浏览公司网站&…