美文网首页
自定义button(链式语法)

自定义button(链式语法)

作者: 苦修士 | 来源:发表于2018-01-03 13:43 被阅读0次

    简单的撸一个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,后期还会以此进行进一步整理.

    相关文章

      网友评论

          本文标题:自定义button(链式语法)

          本文链接:https://www.haomeiwen.com/subject/siehnxtx.html