iOS自定义UIButton的实现

本文首发地址

最近的项目中遇见的自定义的UIButton的情况出现很多种。有的时候觉得要是放几个控件一拼接也可以达到效果。似乎有点太low!!!于是乎就自定义自己的button了。

把title和image位置横向显示

把till和image竖向显示

还有一些其他的情况就看自己心情了。(主要看项目需求了) 看自定义的button如何调用 1:效果图1的代码调用

// 中间按钮
    TitleButton *titleButton = [TitleButton titleButton];
    
    [titleButton setImage:[UIImage imageNamed:@"navigationbar_arrow_down"] forState:UIControlStateNormal];
    [titleButton setTitle:@"北京有限公司" forState:UIControlStateNormal];
    titleButton.frame = CGRectMake(0, 0, 180, 40);
    [titleButton addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.titleView = titleButton;
复制代码

2:效果图2的代码调用

SelleButton * btn = [SelleButton selleButton];
    btn.frame = CGRectMake(100, 100, 80, 100);
    [btn setTitle:@"张三" forState:UIControlStateNormal];
    //[btn setBackgroundImage:[UIImage imageNamed:@"2bj"] forState:UIControlStateHighlighted];
    [self.view addSubview:btn];
复制代码

看代码如何实现: 1:vc的.h文件如下

#import <UIKit/UIKit.h>  
@interface MarkButton : UIButton  
+ (instancetype)markButton;  
@end 
复制代码

2:vc的.m文件如下

#define MarkButtonTitleW 40

#import "MarkButton.h"

@implementation MarkButton

+ (instancetype)markButton
{
    return [[self alloc] init];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // 高亮的时候不要自动调整图标
        self.adjustsImageWhenHighlighted = NO;
        self.titleLabel.font = [UIFont boldSystemFontOfSize:15];
        //self.titleLabel.textColor = [UIColor whiteColor];
        self.imageView.contentMode = UIViewContentModeCenter;
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        // 背景
        [self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }
    return self;
}

- (CGRect)imageRectForContentRect:(CGRect)contentRect
{
    CGFloat imageY = 0;
    CGFloat imageX = 0;
    CGFloat imageW = contentRect.size.width - MarkButtonTitleW;
    CGFloat imageH = contentRect.size.height;
    return CGRectMake(imageX, imageY, imageW, imageH);
}

- (CGRect)titleRectForContentRect:(CGRect)contentRect
{
    CGFloat titleY = 0;
    CGFloat titleW = MarkButtonTitleW;
    CGFloat titleX = contentRect.size.width - titleW;
    CGFloat titleH = contentRect.size.height;
    return CGRectMake(titleX, titleY, titleW, titleH);
}
@end
复制代码

3:详细介绍如下 查阅api系统官方给我我们这几个方法

- (CGRect)backgroundRectForBounds:(CGRect)bounds;
- (CGRect)contentRectForBounds:(CGRect)bounds;
- (CGRect)titleRectForContentRect:(CGRect)contentRect;
- (CGRect)imageRectForContentRect:(CGRect)contentRect;
复制代码

3.1重写Button的Title

- (CGRect)titleRectForContentRect:(CGRect)contentRect;  
复制代码

这里的contentRect就是你当前在使用自定义Button的title 的width和height。我们重写这个方法,可以让button的title的位置变换位置和改变大小。 3.2 重写Button的Image

- (CGRect)imageRectForContentRect:(CGRect)contentRect;
复制代码

这里的contentRect就是你当前在使用自定义Button的image的width和height。我们重写这个方法,可以让button的image的位置变换和改变大小。

3.3 剩下的两个方法就看各位了。 (提高装逼能力!!!)

如有问题可添加我的QQ:1290925041 还可添加QQ群:234812704(洲洲哥) 欢迎各位一块学习,提高逼格! 也可以添加洲洲哥的微信公众号 添加微信公众号最快最好的收听

转载于:https://juejin.im/post/5c4c10e551882524492347fd

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值