iOS开发 - 封装一个自己的按钮

有时候,你会看到设计出来的界面某个位置可点击,但是直接用按钮又无法控制几个元素的关系,这个时候与其用多个控件组合出来这样的按钮不如自己封装一个来的快,还可以重复使用。虽然也需要计算元素的位置大小,但是多次使用的特性对于代码的优化起到至关重要的作用,看看博主要写的按钮长什么样:

这里写图片描述

这样一长条的可点击区域,左右线条长短一致且垂直居中,中间为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下载地址:点击下载

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CodingFire

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值