砖搬久了,慢慢会发现很多时间总会在做些重复的事情,这个时候我们就会习惯性的去考虑怎么去省事,这也是我之所以突然想到用链式编程的原因,废话不多说,先举个例子,以初始化一个UIButton为例来看看链式编程的效果:
1、普通方案
UIButton *button =[UIButton buttonWithType:UIButtonTypeCustom];
button.frame=CGRectMake(0, 0, 105, 30);
[button setBackgroundColor:RGB(255,255,255) forState:UIControlStateNormal];
[button setTitle:@"title" forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"drop_up"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"drop_down"] forState:UIControlStateSelected];
[button setTitleColor:RGB(51,51,51) forState:UIControlStateNormal];
[button setTitleColor:RGB(51,51,51) forState:UIControlStateSelected];
button.titleLabel.font=[UIFont systemFontOfSize:14];
2、链式编程方案
UIButton *button =[UIButton buttonWithType:UIButtonTypeCustom];
button.normalTitleColor(RGB(51,51,51)).selectedTitleColor(RGB(51,51,51)).normalBackgroundColor(RGB(255,255,255)).selectedBackgroundColor(RGB(255,255,255)).normalImage(@"drop_up").selectedImage(@"drop_down").normalTitle(@"title");
总结:
个人觉得第二种方案在实际开发中使用会比第一种稍微便捷一点,每次设置对应属性的时候直接使用点语法就可以了,而且还可以一直往后面点,也可以分开单个使用点语法,不需要每次去调用系统UIButton的set方法,而且还要去输入当前的状态,
以上为个人意见不喜勿碰,谢谢!
👇接下来看看封装的代码的真面目吧--------
创建一个UIButton的类别,下面附上.h、.m文件中实现代码,感兴趣的可以参照我写的几个拓展出更多来,如果有更简便的方案欢迎联系我一起沟通学习 🤝🤝
UIButton+Button.h
#import <UIKit/UIKit.h>
@interface UIButton (Button)
/**
字体大小
*/
-(UIButton *(^)(CGFloat ))butTexFont;
/**
默认文字
*/
-(UIButton *(^)(NSString *))normalTitle;
/**
点击文字
*/
-(UIButton *(^)(NSString *))selectedTitle;
/**
默认背景颜色
*/
-(UIButton *(^)(UIColor *))normalBackgroundColor;
/**
点击背景颜色
*/
-(UIButton *(^)(UIColor *))selectedBackgroundColor;
/**
默认文字颜色
*/
-(UIButton *(^)(UIColor *))normalTitleColor;
/**
点击文字颜色
*/
-(UIButton *(^)(UIColor *))selectedTitleColor;
/**
默认image
*/
-(UIButton *(^)(NSString *))normalImage;
/**
点击image
*/
-(UIButton *(^)(NSString *))selectedImage;
@end
UIButton+Button.m
#import "UIButton+Button.h"
@implementation UIButton (Button)
#pragma mark -- 字体大小
-(UIButton *(^)(CGFloat))butTexFont
{
return ^id(CGFloat butTexFont)
{
self.titleLabel.font=[UIFont systemFontOfSize:butTexFont];
return self;
};
}
#pragma mark --默认文字
-(UIButton *(^)(NSString *))normalTitle{
return ^id(NSString * normalTitle){
[self setTitle:normalTitle forState:UIControlStateNormal];
return self;
};
}
#pragma mark ---点击文字
-(UIButton *(^)(NSString *))selectedTitle{
return ^id(NSString * selectedTitle){
[self setTitle:selectedTitle forState:UIControlStateSelected];
return self;
};
}
#pragma mark --默认image
-(UIButton *(^)(NSString *))normalImage{
return ^id(NSString * normalImage){
[self setImage:[UIImage imageNamed:normalImage] forState:UIControlStateNormal];
return self;
};
}
#pragma mark ---点击image
-(UIButton *(^)(NSString *))selectedImage{
return ^id(NSString * selectedImage){
[self setImage:[UIImage imageNamed:selectedImage] forState:UIControlStateSelected];
return self;
};
}
#pragma mark --默认背景颜色
-(UIButton *(^)(UIColor *))normalBackgroundColor{
return ^id(UIColor * normalBackgroundColor){
[self setBackgroundColor:normalBackgroundColor forState:UIControlStateNormal];
return self;
};
}
#pragma mark --点击背景颜色
-(UIButton *(^)(UIColor *))selectedBackgroundColor{
return ^id(UIColor * selectedBackgroundColor){
[self setBackgroundColor:selectedBackgroundColor forState:UIControlStateSelected];
return self;
};
}
#pragma mark ---默认文字颜色
-(UIButton *(^)(UIColor *))normalTitleColor{
return ^id(UIColor * normalTitleColor){
[self setTitleColor:normalTitleColor forState:UIControlStateNormal];
return self;
};
}
#pragma mark ---点击文字颜色
-(UIButton *(^)(UIColor *))selectedTitleColor{
return ^id(UIColor * selectedTitleColor){
[self setTitleColor:selectedTitleColor forState:UIControlStateSelected];
return self;
};
}
@end