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
原创粉丝点击