iOS 如何讓自己的代碼高端一些之鏈?zhǔn)椒椒▌?chuàng)建基礎(chǔ)控件

最近看自己寫的代碼總覺的很low,特別是創(chuàng)建基礎(chǔ)控件的時候,而且非常麻煩,于是乎研究了一下鏈?zhǔn)骄幊?,?jīng)過一番探索,借鑒了前輩們的經(jīng)驗,整合了一下,封裝了幾個基礎(chǔ)控件分享一下。
廢話不多說,先上一下最終調(diào)用結(jié)果
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(@"正常狀態(tài)")
.lxy_selectedTitle(@"選中狀態(tài)")
.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);

這樣可以實現(xiàn)一句代碼完成所有創(chuàng)建和賦值,可以說非常簡單方便了,下面說一下思路。其實很簡單,封裝這個幾個基礎(chǔ)控件歸根結(jié)底用到的是block作為返回值的結(jié)構(gòu),也就是說賦值完成之后返回控件本身,可以實現(xiàn)無限打點調(diào)用的方法的一種形式。下面貼一下UILabel的實現(xiàn),其他的同理。

@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

有什么不對的地方,歡迎指正!

嗯,點個贊再走啊~

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容