美文网首页
自定义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(链式语法)

    简单的撸一个demo,里面包含枚举、链式语法、语法糖。这是.h文件 .m文件 使用的时候: 只是一个很简单的dem...

  • Objective-C实现链式编程语法(DSL)

    Objective-C实现链式编程语法(DSL) Objective-C实现链式编程语法(DSL)

  • 第一篇:链式语法实现分析

    引言 第三方自动布局框架 Masonry 的出现,是链式语法的鼻祖,也让我们见识到链式语法的魅力! 那么链式语法是...

  • 链式语法学习

    为什么要写一个链式语法的库 最近看了 JHChainableAnimations 和 Masonry 觉得链式语法...

  • Underscore.js 源码解读之链式语法

    链式语法写法 我们调用多个 Underscore 的方法。可能会这么写: 用 Underscore 的链式语法来简...

  • iOS bug 记录

    1、自定义UIButton,自定义button中添加UIImageView和UILabel。设置button的en...

  • Objective-C 链式编程简单实现

    链式编程 链式编程,其实就是通过.点语法将多行代码链接成一句代码,书写简洁、可读性好,但代码语法苦涩难懂。关于链式...

  • 链式语法

    masonry里面自动布局使用的.top().bottom();就是链式语法 (BaseViewControlle...

  • 链式语法

    谈到链式编程,那Masonry几乎就是最经典的代表: make.top.equalTo(self.view).of...

  • Swift链式调用的安全性

    文中引用到的:SnapKitAlamofireBees 链式语法在开发很常见。iOS上的许多开源库都使用了链式语法...

网友评论

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

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