iOS开发 - 封装一个自己的按钮
来源:互联网 发布:搜索排名优化策划 编辑:程序博客网 时间:2024/06/03 18:52
有时候,你会看到设计出来的界面某个位置可点击,但是直接用按钮又无法控制几个元素的关系,这个时候与其用多个控件组合出来这样的按钮不如自己封装一个来的快,还可以重复使用。虽然也需要计算元素的位置大小,但是多次使用的特性对于代码的优化起到至关重要的作用,看看博主要写的按钮长什么样:
这样一长条的可点击区域,左右线条长短一致且垂直居中,中间为logo和对应标题,且多个按钮的情况下按钮长短一样,线条根据内容长短变化。
1.计算内容长度;
2.确定logo尺寸或者相对尺寸;
3.按钮长度减去1,2中的长度除以2就是线条长度;
4.为了美观,减少线条长度,中间留白。
下面看代码:
#import <UIKit/UIKit.h>@interface LHButton : UIButton- (void)setMyButtonIcon:(UIImage *)image titleTest:(NSString *)title titleFont:(CGFloat)fontSize titleColor:(UIColor *)color;@end#import "LHButton.h"@implementation LHButton/*// Only override drawRect: if you perform custom drawing.// An empty implementation adversely affects performance during animation.- (void)drawRect:(CGRect)rect { // Drawing code}*/- (void)setMyButtonIcon:(UIImage *)image titleTest:(NSString *)title titleFont:(CGFloat)fontSize titleColor:(UIColor *)color{ CGFloat titleWidth = [self configureLength:title titleFont:fontSize]; UIView *lineLeft = [[UIView alloc]initWithFrame:CGRectMake(0, self.frame.size.height/2, (self.frame.size.width - titleWidth - 15 - 13)/2, 1)]; lineLeft.backgroundColor = [UIColor colorWithRed:0.84f green:0.83f blue:0.80f alpha:1.00f]; [self addSubview:lineLeft]; UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake((self.frame.size.width - titleWidth - 15 - 13)/2 + 5, (self.frame.size.height - 13)/2, 13, 13)]; imageView.image = image; [self addSubview:imageView]; UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake((self.frame.size.width - titleWidth - 15 - 13)/2 + 3 + 13 + 3, 0, titleWidth + 9, self.frame.size.height)]; label.text = title; label.textAlignment = NSTextAlignmentCenter; label.font = [UIFont systemFontOfSize:fontSize]; label.textColor = color; [self addSubview:label]; UIView *lineRight = [[UIView alloc]initWithFrame:CGRectMake(self.frame.size.width - (self.frame.size.width - titleWidth - 15 - 13)/2, self.frame.size.height/2, (self.frame.size.width - titleWidth - 15 - 13)/2, 1)]; lineRight.backgroundColor = [UIColor colorWithRed:0.84f green:0.83f blue:0.80f alpha:1.00f]; [self addSubview:lineRight];}#pragma mark - 计算长度- (NSInteger)configureLength:(NSString *)titleTest titleFont:(CGFloat)fontSize{ NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:fontSize]}; CGSize size = [titleTest boundingRectWithSize:CGSizeMake(1000, 0) options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute context:nil].size; return size.width;}@end
然后直接在VC中引用头文件后使用:
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. LHButton *myBtn = [LHButton buttonWithType:UIButtonTypeCustom]; myBtn.frame = CGRectMake(20, 100, 320 - 40, 40); [myBtn addTarget:self action:@selector(myBtnAction) forControlEvents:UIControlEventTouchUpInside]; [myBtn setMyButtonIcon:[UIImage imageNamed:@"icon-email.png"] titleTest:@"Sign up with Email" titleFont:13 titleColor:[UIColor colorWithRed:0.49f green:0.48f blue:0.46f alpha:1.00f]]; [self.view addSubview:myBtn];}- (void)myBtnAction{ NSLog(@"This is my Button");}
如果你想要别的样式的按钮,换汤不换药,都可以这么来搞。
Demo下载地址:点击下载
2 0
- iOS开发 - 封装一个自己的按钮
- 自己封装的一个IOS的RadioButton
- 自己封装的一个基于ios的一个折叠tableView
- iOS开发封装篇-利用CAShapeLayer和绘图封装一个动画按钮
- 学习ios 自己创建一个按钮,NSUserDefault的用法
- iOS开发——封装自己的下拉菜单
- ios开发: 按钮定时器方法封装
- iOS——分享自己封装的验证码按钮控件CFSecurityCodeButton
- ios开发--地址选择封装的一个demo
- iOS开发造轮子 | 优雅的封装一个倒计时button
- iOS开发 封装一个可以复用的弹窗父类(TSActionAlertView)
- ios按钮事件的block封装
- iOS按钮封装
- iOS开发封装篇-利用绘图封装一个可以涂鸦的view
- 一个iOS开发者业余开发自己APP的经验分享
- iOS 开发之封装一个 sqlite3 数据库
- ios-新浪微博开发-22-封装标题按钮
- iOS FMDB自己封装的单例类
- nginx根目录重定向到首页
- Java协程框架----Kilim实现机制解析
- play 集成 Ajax请求
- MYSQL相关
- poj1836
- iOS开发 - 封装一个自己的按钮
- 开发者都应该使用的10个C++11特性
- 面试如何利用最后五分钟反败为胜
- CKEditor 4 开发人员指南
- Hibernate 实体关联关系映射----总结
- Android基础组件-ContentProvider(二)
- PageRank算法
- 二分查找:Search for a Range
- CheckStyle的检查规则(共138条规则)