最近看自己寫的代碼總覺的很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
有什么不對的地方润匙,歡迎指正!
嗯饼丘,點個贊再走啊~