iOS 如何让自己的代码高端一些之链式方法创建基础控件

2021-02-26  本文已影响0人  三千沉浮
最近看自己写的代码总觉的很low,特别是创建基础控件的时候,而且非常麻烦,于是乎研究了一下链式编程,经过一番探索,借鉴了前辈们的经验,整合了一下,封装了几个基础控件分享一下。
废话不多说,先上一下最终调用结果
UIView *view = UIView.lxy_new()
.lxy_frame(100,200,100,60)
.lxy_backgroundColor(UIColor.orangeColor)
.lxy_masksToBounds(YES)
.lxy_cornerRadius(5)
.lxy_borderColor(UIColor.purpleColor.CGColor)
.lxy_borderWidth(1)
.lxy_addGestureRecognizer(self,@selector(tapGesture:));
self.view.addSubview(view);
    
UIButton *button = UIButton.lxy_new()
.lxy_frame(100,300,100,60)
.lxy_title(@"正常状态")
.lxy_selectedTitle(@"选中状态")
.lxy_titleColor(UIColor.blackColor)
.lxy_selectedTextColor(UIColor.redColor)
.lxy_target(self,@selector(btnAction:),UIControlEventTouchUpInside)
.lxy_backgroundColor(UIColor.yellowColor)
.lxy_masksToBounds(YES)
.lxy_cornerRadius(5)
.lxy_borderColor(UIColor.blueColor.CGColor)
.lxy_borderWidth(1);
self.view.addSubview(button);
    
UILabel *label = UILabel.lxy_new()
.lxy_frame(100,400,100,60)
.lxy_text(@"文字")
.lxy_font([UIFont systemFontOfSize:13])
.lxy_textColor(UIColor.blackColor)
.lxy_textAlignment(NSTextAlignmentCenter)
.lxy_numOfLines(0)
.lxy_backgroundColor(UIColor.cyanColor)
.lxy_masksToBounds(YES)
.lxy_cornerRadius(5)
.lxy_addGestureRecognizer(self,@selector(tapGesture:));
self.view.addSubview(label);

这样可以实现一句代码完成所有创建和赋值,可以说非常简单方便了,下面说一下思路。其实很简单,封装这个几个基础控件归根结底用到的是block作为返回值的结构,也就是说赋值完成之后返回控件本身,可以实现无限打点调用的方法的一种形式。下面贴一下UILabel的实现,其他的同理。

@interface UILabel (ChainProgram)

+ (UILabel *(^)(void))lxy_new;
- (UILabel *(^)(CGFloat x,CGFloat y,CGFloat width,CGFloat height))lxy_frame;
- (UILabel *(^)(NSString *text))lxy_text;
- (UILabel *(^)(NSAttributedString *attrText))lxy_attrText;
- (UILabel *(^)(UIFont *font))lxy_font;
- (UILabel *(^)(UIColor *textColor))lxy_textColor;
- (UILabel *(^)(NSInteger numberOfLines))lxy_numOfLines;
- (UILabel *(^)(NSTextAlignment textAlignment))lxy_textAlignment;

- (UILabel *(^)(UIColor *backgroundColor))lxy_backgroundColor;
- (UILabel *(^)(CGFloat cornerRadius))lxy_cornerRadius;
- (UILabel *(^)(BOOL masksToBounds))lxy_masksToBounds;
- (UILabel *(^)(CGColorRef borderColor))lxy_borderColor;
- (UILabel *(^)(CGFloat borderWidth))lxy_borderWidth;

- (UILabel *(^)(id target, SEL sel))lxy_addGestureRecognizer;

@end

#import "UILabel+ChainProgram.h"

@implementation UILabel (ChainProgram)

+ (UILabel *(^)(void))lxy_new{
    return ^(void){
        return [UILabel new];
    };
}

- (UILabel *(^)(CGFloat x,CGFloat y,CGFloat width,CGFloat height))lxy_frame{
    return ^(CGFloat x,CGFloat y,CGFloat width,CGFloat height){
        CGRect frame = self.frame;
        frame.origin.x = x;
        frame.origin.y = y;
        frame.size.width = width;
        frame.size.height = height;
        self.frame = frame;
        return self;
    };
}
- (UILabel *(^)(NSString *text))lxy_text{
    return ^(NSString *text){
        self.text = text;
        return self;
    };
}
- (UILabel *(^)(NSAttributedString *attrText))lxy_attrText{
    return ^(NSAttributedString *attrText){
        self.attributedText = attrText;
        return self;
    };
}
- (UILabel *(^)(UIFont *font))lxy_font{
    return ^(UIFont *font){
        self.font = font;
        return self;
    };
}
- (UILabel *(^)(UIColor *textColor))lxy_textColor{
    return ^(UIColor *textColor){
        self.textColor = textColor;
        return self;
    };
}
- (UILabel *(^)(NSInteger numberOfLines))lxy_numOfLines{
    return ^(NSInteger numberOfLines){
        self.numberOfLines = numberOfLines;
        return self;
    };
}
- (UILabel *(^)(NSTextAlignment textAlignment))lxy_textAlignment{
    return ^(NSTextAlignment textAlignment){
        self.textAlignment = textAlignment;
        return self;
    };
}

- (UILabel *(^)(UIColor *backgroundColor))lxy_backgroundColor{
    return ^(UIColor *backgroundColor){
        self.backgroundColor = backgroundColor;
        return self;
    };
}
- (UILabel *(^)(CGFloat cornerRadius))lxy_cornerRadius{
    return ^(CGFloat cornerRadius){
        self.layer.cornerRadius = cornerRadius;
        return self;
    };
}
- (UILabel *(^)(BOOL masksToBounds))lxy_masksToBounds{
    return ^(BOOL masksToBounds){
        self.layer.masksToBounds = masksToBounds;
        return self;
    };
}
- (UILabel *(^)(CGColorRef borderColor))lxy_borderColor{
    return ^(CGColorRef borderColor){
        self.layer.borderColor = borderColor;
        return self;
    };
}
- (UILabel *(^)(CGFloat borderWidth))lxy_borderWidth{
    return ^(CGFloat borderWidth){
        self.layer.borderWidth = borderWidth;
        return self;
    };
}

- (UILabel *(^)(id target, SEL sel))lxy_addGestureRecognizer{
    return ^(id target, SEL sel){
        self.userInteractionEnabled = YES;
        UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc]initWithTarget:target action:sel];
        [self addGestureRecognizer:gesture];
        return self;
    };
}
@end

有什么不对的地方,欢迎指正!

嗯,点个赞再走啊~

上一篇下一篇

猜你喜欢

热点阅读