自定义button(链式语法)

简单的撸一个demo,里面包含枚举、链式语法、语法糖。
这是.h文件

#import <UIKit/UIKit.h>
//枚举
typedef NS_ENUM(NSUInteger,ButtonType){
    IMAGETOP    = 1,     //图片在上,文字在下
    IMAGELEFT   = 2,     //图片在左,文字在右
    IMAGERIGHT  = 3,     //图片在右,文字在左
    IMAGEBOTTOM = 4,     //图片在下,文字在上
};

@interface ZYButton : UIButton
@property(nonatomic, strong)UILabel *titleL;
@property(nonatomic, strong)UIImageView *imageV;
/*!按钮样式,返回自身self,以便使用点语法。*/
- (ZYButton *(^)(ButtonType type))btnType;
/*!背景颜色*/
- (ZYButton *(^)(UIColor  *backgroundColor))backColor;
/*!标题*/
- (ZYButton *(^)(NSString *title))title;
/*!图片*/
- (ZYButton *(^)(NSString *image))image;
@end

.m文件

#import "ZYButton.h"
#import "Masonry.h"


@interface ZYButton()
@end
@implementation ZYButton

- (instancetype)init
{
    if (self = [super init]) {
    }
    return self;
}
- (ZYButton *(^)(ButtonType type))btnType{
    return ^(ButtonType type){
        switch (type) {
            case IMAGETOP:
                [self imageTopTitleBottom];
                break;
            case IMAGELEFT:
                [self imageLeftTitleRight];
                break;
            case IMAGERIGHT:
                [self imageRightTitleLeft];
                break;
            case IMAGEBOTTOM:
                [self imageBottomTitleTop];
                break;
            default:
                break;
        }
        return self;
    };
}
- (void)imageTopTitleBottom{
    self.imageV = ({
        UIImageView *imageView = [[UIImageView alloc] init];
        [self addSubview:imageView];
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.mas_top).offset(5);
            make.left.equalTo(self.mas_left).offset(5);
            make.right.equalTo(self.mas_right).offset(-5);
            make.bottom.equalTo(self.mas_top).offset(50);
        }];
        imageView;
    });
    self.titleL = ({
        UILabel *label = [[UILabel alloc] init];
        label.textAlignment=NSTextAlignmentCenter;
        [self addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.imageV.mas_bottom).offset(5);
            make.left.equalTo(self.mas_left).offset(5);
            make.right.equalTo(self.mas_right).offset(-5);
            make.bottom.equalTo(self.mas_bottom).offset(-5);
        }];
        label;
    });
}
- (void)imageLeftTitleRight{
    self.imageV = ({
        UIImageView *imageView = [[UIImageView alloc] init];
        [self addSubview:imageView];
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.mas_top).offset(5);
            make.left.equalTo(self.mas_left).offset(5);
            make.right.equalTo(self.mas_left).offset(35);
            make.bottom.equalTo(self.mas_bottom).offset(-5);
        }];
        imageView;
    });
    self.titleL = ({
        UILabel *label = [[UILabel alloc] init];
        label.textAlignment=NSTextAlignmentCenter;
        [self addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.mas_top).offset(5);
            make.left.equalTo(self.imageV.mas_left).offset(5);
            make.right.equalTo(self.mas_right).offset(-5);
            make.bottom.equalTo(self.mas_bottom).offset(-5);
        }];
        label;
    });
}
- (void)imageRightTitleLeft{
    self.titleL = ({
        UILabel *label = [[UILabel alloc] init];
        label.textAlignment=NSTextAlignmentCenter;
        [self addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.mas_top).offset(5);
            make.left.equalTo(self.mas_left).offset(5);
            make.right.equalTo(self.mas_left).offset(50);
            make.bottom.equalTo(self.mas_bottom).offset(-5);
        }];
        label;
    });
    self.imageV = ({
        UIImageView *imageView = [[UIImageView alloc] init];
        [self addSubview:imageView];
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.mas_top).offset(5);
            make.left.equalTo(self.titleL.mas_right).offset(5);
            make.right.equalTo(self.mas_right).offset(-5);
            make.bottom.equalTo(self.mas_bottom).offset(-5);
        }];
        imageView;
    });

}
- (void)imageBottomTitleTop{
    self.titleL = ({
        UILabel *label = [[UILabel alloc] init];
        label.textAlignment=NSTextAlignmentCenter;
        [self addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.mas_top).offset(5);
            make.left.equalTo(self.mas_left).offset(5);
            make.right.equalTo(self.mas_right).offset(-5);
            make.bottom.equalTo(self.mas_top).offset(35);
        }];
        label;
    });
    self.imageV = ({
        UIImageView *imageView = [[UIImageView alloc] init];
        [self addSubview:imageView];
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.titleL.mas_bottom).offset(5);
            make.left.equalTo(self.mas_left).offset(5);
            make.right.equalTo(self.mas_right).offset(-5);
            make.bottom.equalTo(self.mas_bottom).offset(-5);
        }];
        imageView;
    });
    
}

- (ZYButton *(^)(UIColor  *backgroundColor))backColor{
    return  ^(UIColor *backColor){
        self.backgroundColor = backColor;
        return self;
    };
}
- (ZYButton *(^)(NSString *title))title{
    return  ^(NSString *title){
        self.titleL.text = title;
        return self;
    };
}
- (ZYButton *(^)(NSString *image))image{
    return ^(NSString *image){
        self.imageV.image = [UIImage imageNamed:image];
        return self;
    };
}
@end

使用的时候:

    ZYButton *button = [[ZYButton alloc]init];
    //链式语法
    button.backColor([UIColor blueColor]).btnType(IMAGETOP).image(@"test.png").title(@"测试"); 
    button.titleL.backgroundColor = [UIColor redColor];
    [self.view addSubview:button];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(5);
        make.left.equalTo(self.view.mas_left).offset(5);
        make.size.mas_equalTo(CGSizeMake(100, 100));
    }];

只是一个很简单的demo,后期还会以此进行进一步整理.

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,866评论 25 709
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,312评论 4 61
  • 上周和这周画了两次黑色的罐子! 两张对比着看,是不一样的。 罐子还是那个罐子,不同的是心里的想象图形!有本书叫《写...
    依然如水阅读 318评论 0 0
  • 听说,有一种活法叫做人要单纯些思想要成熟些,开始我还想反驳他的矛盾,思想成熟的人怎么会单纯呢?但后来想想...
    teSHIRLY阅读 682评论 3 4
  • 1.一直在想,怎么帮我父亲戒除近30年的烟瘾?今天突然灵机一动:如果每天固定零点在微信给他发一个“每当你想抽烟时,...
    琢磨概念者阅读 926评论 1 2