OC:链式编程

概念:

链式编程:将多个业务逻辑(方法)通过“.”(点号)串联起来的一种代码风格,形似链条,故称链式编程。核心思想在于每个方法均返回自身实例。

Function+Extension.h
#import <UIKit/UIKit.h>

//MARK: - UILabel
@interface UILabel (Extension)

+ (instancetype)label;

- (UILabel *(^)(NSString *))name;
- (UILabel *(^)(UIColor *))nameColor;
- (UILabel *(^)(NSString *))nameColorHex;
- (UILabel *(^)(UIFont *))nameFont;
- (UILabel *(^)(NSInteger))lines;
- (UILabel *(^)(NSLineBreakMode))breakMode;
- (UILabel *(^)(NSTextAlignment))align;
- (UILabel *(^)(UIView *))addToView;
- (UILabel *(^)(CGRect))rect;
- (UILabel *(^)(UIColor *))bgColor;
- (UILabel *(^)(NSString *))bgColorHex;
- (UILabel *(^)(CGFloat))cornerRadius;
- (UILabel *(^)(CGFloat))borderWidth;
- (UILabel *(^)(UIColor *))borderColor;
- (UILabel *(^)(NSString *))borderColorHex;

@end


//MARK: - UIView
@interface UIView (Extension)

+ (instancetype)view;

- (UIView *(^)(UIView *))addToView;
- (UIView *(^)(CGRect))rect;
- (UIView *(^)(UIColor *))bgColor;
- (UIView *(^)(NSString *))bgColorHex;
- (UIView *(^)(CGFloat))cornerRadius;
- (UIView *(^)(CGFloat))borderWidth;
- (UIView *(^)(UIColor *))borderColor;
- (UIView *(^)(NSString *))borderColorHex;

@end


//MARK: - UIButton
@interface UIButton (Extension)

- (UIButton *(^)(NSString *, UIControlState state))name;
- (UIButton *(^)(UIColor *, UIControlState state))nameColor;
- (UIButton *(^)(NSString *, UIControlState state))nameColorHex;
- (UIButton *(^)(UIFont *))nameFont;
- (UIButton *(^)(UIImage *, UIControlState state))image;
- (UIButton *(^)(UIView *))addToView;
- (UIButton *(^)(CGRect))rect;
- (UIButton *(^)(UIColor *))bgColor;
- (UIButton *(^)(NSString *))bgColorHex;
- (UIButton *(^)(CGFloat))cornerRadius;
- (UIButton *(^)(CGFloat))borderWidth;
- (UIButton *(^)(UIColor *))borderColor;
- (UIButton *(^)(NSString *))borderColorHex;
- (UIButton *(^)(id, SEL, UIControlEvents))selector;

@end


//MARK: - UITextField
@interface UITextField (Extension)

+ (instancetype)textField;

- (UITextField *(^)(NSString *))name;
- (UITextField *(^)(UIColor *))nameColor;
- (UITextField *(^)(NSString *))nameColorHex;
- (UITextField *(^)(UIFont *))nameFont;
- (UITextField *(^)(UIView *))addToView;
- (UITextField *(^)(CGRect))rect;
- (UITextField *(^)(UIColor *))bgColor;
- (UITextField *(^)(NSString *))bgColorHex;
- (UITextField *(^)(NSString *))placeText;
- (UITextField *(^)(UIKeyboardType))keyboard;
- (UITextField *(^)(NSTextAlignment))align;
- (UITextField *(^)(UIReturnKeyType))returnKey;
- (UITextField *(^)(UITextFieldViewMode))clearButton;
- (UITextField *(^)(UITextBorderStyle))borderType;
- (UITextField *(^)(UIColor *))cursorColor;
- (UITextField *(^)(NSString *))cursorColorHex;

@end
Function+Extension.m
#import "Function+Extension.h"

//MARK: - UIColor
@implementation UIColor (Extension)

+ (UIColor *)colorWithHex:(NSString *)hexColor {
    
    if ([hexColor length] < 6) {
        return nil;
    }
    
    unsigned int red, green, blue;
    NSRange range;
    range.length = 2;
    range.location = 0;
    [[NSScanner scannerWithString:[hexColor substringWithRange:range]] scanHexInt:&red];
    range.location = 2;
    [[NSScanner scannerWithString:[hexColor substringWithRange:range]] scanHexInt:&green];
    range.location = 4;
    [[NSScanner scannerWithString:[hexColor substringWithRange:range]] scanHexInt:&blue];
    return [UIColor colorWithRed:(float)(red/255.f) green:(float)(green / 255.f) blue:(float)(blue / 255.f) alpha:1.f];
}

@end


//MARK: - UILabel
@implementation UILabel (Extension)

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

- (UILabel *(^)(NSString *))name {
    return ^(NSString *name) {
        self.text = name;
        return self;
    };
}

- (UILabel *(^)(UIColor *))nameColor {
    return ^(UIColor *nameColor) {
        self.textColor = nameColor;
        return self;
    };
}

- (UILabel *(^)(NSString *))nameColorHex {
    return ^(NSString *nameColorHex) {
        self.textColor = [UIColor colorWithHex:nameColorHex];
        return self;
    };
}

- (UILabel *(^)(UIFont *))nameFont {
    return ^(UIFont *nameFont) {
        self.font = nameFont;
        return self;
    };
}

- (UILabel *(^)(NSInteger))lines {
    return ^(NSInteger lines) {
        self.numberOfLines = lines;
        return self;
    };
}

- (UILabel *(^)(NSLineBreakMode))breakMode {
    return ^(NSLineBreakMode breakMode) {
        self.lineBreakMode = breakMode;
        return self;
    };
}

- (UILabel *(^)(NSTextAlignment))align {
    return ^(NSTextAlignment align) {
        self.textAlignment = align;
        return self;
    };
}

- (UILabel *(^)(UIView *))addToView {
    return ^(UIView *superView) {
        [superView addSubview:self];
        return self;
    };
}

- (UILabel *(^)(CGRect))rect {
    return ^(CGRect rect) {
        self.frame = rect;
        return self;
    };
}

- (UILabel *(^)(UIColor *))bgColor {
    return ^(UIColor *bgColor) {
        self.layer.backgroundColor = bgColor.CGColor;
        return self;
    };
}

- (UILabel *(^)(NSString *))bgColorHex {
    return ^(NSString *bgColorHex) {
        self.layer.backgroundColor = [UIColor colorWithHex:bgColorHex].CGColor;
        return self;
    };
}

- (UILabel *(^)(UIColor *))borderColor {
    return ^(UIColor *borderColor) {
        self.layer.borderColor = borderColor.CGColor;
        return self;
    };
}

- (UILabel *(^)(NSString *))borderColorHex {
    return ^(NSString *borderColorHex) {
        self.layer.borderColor = [UIColor colorWithHex:borderColorHex].CGColor;
        return self;
    };
}

- (UILabel *(^)(CGFloat))borderWidth {
    return ^(CGFloat borderWidth) {
        self.layer.borderWidth = borderWidth;
        return self;
    };
}

- (UILabel *(^)(CGFloat))cornerRadius {
    return ^(CGFloat cornerRadius) {
        self.layer.cornerRadius = cornerRadius;
        return self;
    };
}

@end


//MARK: - UIView
@implementation UIView (Extension)

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

- (UIView *(^)(UIView *))addToView {
    return ^(UIView *superView) {
        [superView addSubview:self];
        return self;
    };
}

- (UIView *(^)(CGRect))rect {
    return ^(CGRect rect) {
        self.frame = rect;
        return self;
    };
}

- (UIView *(^)(UIColor *))bgColor {
    return ^(UIColor *bgColor) {
        self.layer.backgroundColor = bgColor.CGColor;
        return self;
    };
}

- (UIView *(^)(NSString *))bgColorHex {
    return ^(NSString *bgColorHex) {
        self.layer.backgroundColor = [UIColor colorWithHex:bgColorHex].CGColor;
        return self;
    };
}

- (UIView *(^)(UIColor *))borderColor {
    return ^(UIColor *borderColor) {
        self.layer.borderColor = borderColor.CGColor;
        return self;
    };
}

- (UIView *(^)(NSString *))borderColorHex {
    return ^(NSString *borderColorHex) {
        self.layer.borderColor = [UIColor colorWithHex:borderColorHex].CGColor;
        return self;
    };
}

- (UIView *(^)(CGFloat))borderWidth {
    return ^(CGFloat borderWidth) {
        self.layer.borderWidth = borderWidth;
        return self;
    };
}

- (UIView *(^)(CGFloat))cornerRadius {
    return ^(CGFloat cornerRadius) {
        self.layer.cornerRadius = cornerRadius;
        return self;
    };
}


@end


//MARK: - UIButton
@implementation UIButton (Extension)

- (UIButton *(^)(NSString *, UIControlState))name {
    return ^(NSString *name, UIControlState state) {
        [self setTitle:name forState:state];
        return self;
    };
}

- (UIButton *(^)(UIImage *, UIControlState))image {
    return ^(UIImage *image, UIControlState state) {
        [self setImage:image forState:state];
        return self;
    };
}

- (UIButton *(^)(UIColor *, UIControlState))nameColor {
    return ^(UIColor *nameColor, UIControlState state) {
        [self setTitleColor:nameColor forState:state];
        return self;
    };
}

- (UIButton *(^)(NSString *, UIControlState))nameColorHex {
    return ^(NSString *nameColorHex, UIControlState state) {
        [self setTitleColor:[UIColor colorWithHex:nameColorHex] forState:state];
        return self;
    };
}

- (UIButton *(^)(UIView *))addToView {
    return ^(UIView *superView) {
        [superView addSubview:self];
        return self;
    };
}

- (UIButton *(^)(CGRect))rect {
    return ^(CGRect rect) {
        self.frame = rect;
        return self;
    };
}

- (UIButton *(^)(UIFont *))nameFont {
    return ^(UIFont *nameFont) {
        self.titleLabel.font = nameFont;
        return self;
    };
}

- (UIButton *(^)(UIColor *))bgColor {
    return ^(UIColor *bgColor) {
        self.layer.backgroundColor = bgColor.CGColor;
        return self;
    };
}

- (UIButton *(^)(NSString *))bgColorHex {
    return ^(NSString *bgColorHex) {
        self.layer.backgroundColor = [UIColor colorWithHex:bgColorHex].CGColor;
        return self;
    };
}

- (UIButton *(^)(UIColor *))borderColor {
    return ^(UIColor *borderColor) {
        self.layer.borderColor = borderColor.CGColor;
        return self;
    };
}

- (UIButton *(^)(NSString *))borderColorHex {
    return ^(NSString *borderColorHex) {
        self.layer.borderColor = [UIColor colorWithHex:borderColorHex].CGColor;
        return self;
    };
}

- (UIButton *(^)(CGFloat))borderWidth {
    return ^(CGFloat borderWidth) {
        self.layer.borderWidth = borderWidth;
        return self;
    };
}

- (UIButton *(^)(CGFloat))cornerRadius {
    return ^(CGFloat cornerRadius) {
        self.layer.cornerRadius = cornerRadius;
        return self;
    };
}

- (UIButton *(^)(id, SEL, UIControlEvents))selector {
    return ^(id target, SEL selector, UIControlEvents style) {
        [self addTarget:target action:selector forControlEvents:style];
        return self;
    };
}

@end


//MARK: - UITextField
@implementation UITextField (Extension)

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

- (UITextField *(^)(NSString *))name {
    return ^(NSString *name) {
        self.text = name;
        return self;
    };
}

- (UITextField *(^)(UIColor *))nameColor {
    return ^(UIColor *nameColor) {
        self.textColor = nameColor;
        return self;
    };
}

- (UITextField *(^)(NSString *))nameColorHex {
    return ^(NSString *nameColorHex) {
        self.textColor = [UIColor colorWithHex:nameColorHex];
        return self;
    };
}

- (UITextField *(^)(UIFont *))nameFont {
    return ^(UIFont *nameFont) {
        self.font = nameFont;
        return self;
    };
}

- (UITextField *(^)(UIView *))addToView {
    return ^(UIView *superView) {
        [superView addSubview:self];
        return self;
    };
}

- (UITextField *(^)(CGRect))rect {
    return ^(CGRect rect) {
        self.frame = rect;
        return self;
    };
}

- (UITextField *(^)(UIColor *))bgColor {
    return ^(UIColor *bgColor) {
        self.layer.backgroundColor = bgColor.CGColor;
        return self;
    };
}

- (UITextField *(^)(NSString *))bgColorHex {
    return ^(NSString *bgColorHex) {
        self.layer.backgroundColor = [UIColor colorWithHex:bgColorHex].CGColor;
        return self;
    };
}

- (UITextField *(^)(UIKeyboardType))keyboard {
    return ^(UIKeyboardType keyboard) {
        self.keyboardType = keyboard;
        return self;
    };
}

- (UITextField *(^)(NSTextAlignment))align {
    return ^(NSTextAlignment align) {
        self.textAlignment = align;
        return self;
    };
}

- (UITextField *(^)(NSString *))placeText {
    return ^(NSString *placeText) {
        self.placeholder = placeText;
        return self;
    };
}

- (UITextField *(^)(UIReturnKeyType))returnKey {
    return ^(UIReturnKeyType returnKey) {
        self.returnKeyType = returnKey;
        return self;
    };
}

- (UITextField *(^)(UITextFieldViewMode))clearButton {
    return ^(UITextFieldViewMode clearButton) {
        self.clearButtonMode = clearButton;
        return self;
    };
}

- (UITextField *(^)(UITextBorderStyle))borderType {
    return ^(UITextBorderStyle borderType) {
        self.borderStyle = borderType;
        return self;
    };
}

- (UITextField *(^)(UIColor *))cursorColor {
    return ^(UIColor *cursorColor) {
        self.tintColor = cursorColor;
        return self;
    };
}

- (UITextField *(^)(NSString *))cursorColorHex {
    return ^(NSString *cursorColorHex) {
        self.tintColor = [UIColor colorWithHex:cursorColorHex];
        return self;
    };
}

@end
示例:
#import "ViewController.h"

#import "Function+Extension.h"

@interface ViewController ()


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //MARK: - UILabel
    [UILabel label]
    .name(@"链式编程")
    .nameColorHex(@"ddb86b")
    .align(NSTextAlignmentCenter)
    .bgColor([UIColor grayColor])
    .cornerRadius(10)
    .borderWidth(5)
    .borderColor([UIColor purpleColor])
    .rect(CGRectMake(100, 100, 100, 100))
    .addToView(self.view);
    
    //MARK: - UIView
    [UIView view]
    .bgColor([UIColor redColor])
    .cornerRadius(5)
    .addToView(self.view)
    .rect(CGRectMake(100, 250, 100, 100));

    //MARK: - UIButton
    [UIButton buttonWithType:UIButtonTypeCustom]
    .bgColor([UIColor grayColor])
    .name(@"Normal状态", UIControlStateNormal)
    .name(@"选中状态", UIControlStateSelected)
    .nameColor([UIColor redColor], UIControlStateNormal)
    .addToView(self.view)
    .selector(self, @selector(onTouch:), UIControlEventTouchUpInside)
    .rect(CGRectMake(100, 400, 100, 30));
    
    //MARK: - UITextField
    [UITextField textField]
    .bgColorHex(@"ff0025")
    .name(@"我是TextField")
    .nameColor([UIColor purpleColor])
    .keyboard(UIKeyboardTypePhonePad)
    .addToView(self.view)
    .cursorColor([UIColor yellowColor])
    .rect(CGRectMake(100, 450, 300, 30));
}

- (void)onTouch:(UIButton *)button {
    button.selected = !button.selected;
}

@end
    原文作者:东风快递派送员
    原文地址: https://www.jianshu.com/p/368884d95177
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞