iOS 如何讓自己的代碼高端一些之鏈式方法創(chuàng)建基礎控件

最近看自己寫的代碼總覺的很low,特別是創(chuàng)建基礎控件的時候拐格,而且非常麻煩僧免,于是乎研究了一下鏈式編程,經(jīng)過一番探索捏浊,借鑒了前輩們的經(jīng)驗懂衩,整合了一下,封裝了幾個基礎控件分享一下金踪。
廢話不多說浊洞,先上一下最終調(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)建和賦值,可以說非常簡單方便了胡岔,下面說一下思路法希。其實很簡單,封裝這個幾個基礎控件歸根結(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)系作者
  • 序言:七十年代末趁桃,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子肄鸽,更是在濱河造成了極大的恐慌缺虐,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,372評論 6 498
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件汹想,死亡現(xiàn)場離奇詭異预皇,居然都是意外死亡,警方通過查閱死者的電腦和手機逮诲,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,368評論 3 392
  • 文/潘曉璐 我一進店門帜平,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人梅鹦,你說我怎么就攤上這事裆甩。” “怎么了齐唆?”我有些...
    開封第一講書人閱讀 162,415評論 0 353
  • 文/不壞的土叔 我叫張陵嗤栓,是天一觀的道長。 經(jīng)常有香客問我箍邮,道長茉帅,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,157評論 1 292
  • 正文 為了忘掉前任锭弊,我火速辦了婚禮堪澎,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘味滞。我一直安慰自己樱蛤,他們只是感情好,可當我...
    茶點故事閱讀 67,171評論 6 388
  • 文/花漫 我一把揭開白布剑鞍。 她就那樣靜靜地躺著昨凡,像睡著了一般。 火紅的嫁衣襯著肌膚如雪攒暇。 梳的紋絲不亂的頭發(fā)上土匀,一...
    開封第一講書人閱讀 51,125評論 1 297
  • 那天,我揣著相機與錄音形用,去河邊找鬼就轧。 笑死证杭,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的妒御。 我是一名探鬼主播解愤,決...
    沈念sama閱讀 40,028評論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼乎莉!你這毒婦竟也來了送讲?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,887評論 0 274
  • 序言:老撾萬榮一對情侶失蹤惋啃,失蹤者是張志新(化名)和其女友劉穎哼鬓,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體边灭,經(jīng)...
    沈念sama閱讀 45,310評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡异希,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,533評論 2 332
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了绒瘦。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片称簿。...
    茶點故事閱讀 39,690評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖惰帽,靈堂內(nèi)的尸體忽然破棺而出憨降,到底是詐尸還是另有隱情,我是刑警寧澤该酗,帶...
    沈念sama閱讀 35,411評論 5 343
  • 正文 年R本政府宣布授药,位于F島的核電站,受9級特大地震影響垂涯,放射性物質(zhì)發(fā)生泄漏烁焙。R本人自食惡果不足惜航邢,卻給世界環(huán)境...
    茶點故事閱讀 41,004評論 3 325
  • 文/蒙蒙 一耕赘、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧膳殷,春花似錦操骡、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至勒极,卻和暖如春是掰,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背辱匿。 一陣腳步聲響...
    開封第一講書人閱讀 32,812評論 1 268
  • 我被黑心中介騙來泰國打工键痛, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留炫彩,地道東北人。 一個月前我還...
    沈念sama閱讀 47,693評論 2 368
  • 正文 我出身青樓絮短,卻偏偏與公主長得像江兢,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子丁频,可洞房花燭夜當晚...
    茶點故事閱讀 44,577評論 2 353

推薦閱讀更多精彩內(nèi)容