磚搬久了狈涮,慢慢會發(fā)現(xiàn)很多時間總會在做些重復(fù)的事情授帕,這個時候我們就會習(xí)慣性的去考慮怎么去省事,這也是我之所以突然想到用鏈?zhǔn)骄幊痰脑蚓穑瑥U話不多說黍氮,先舉個例子,以初始化一個UIButton為例來看看鏈?zhǔn)骄幊痰男Ч?/p>
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沫浆、鏈?zhǔn)骄幊谭桨?/p>
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");
總結(jié):
個人覺得第二種方案在實(shí)際開發(fā)中使用會比第一種稍微便捷一點(diǎn),每次設(shè)置對應(yīng)屬性的時候直接使用點(diǎn)語法就可以了滚秩,而且還可以一直往后面點(diǎn)专执,也可以分開單個使用點(diǎn)語法,不需要每次去調(diào)用系統(tǒng)UIButton的set方法郁油,而且還要去輸入當(dāng)前的狀態(tài)本股,
以上為個人意見不喜勿碰攀痊,謝謝!
??接下來看看封裝的代碼的真面目吧--------
創(chuàng)建一個UIButton的類別拄显,下面附上.h苟径、.m文件中實(shí)現(xiàn)代碼,感興趣的可以參照我寫的幾個拓展出更多來躬审,如果有更簡便的方案歡迎聯(lián)系我一起溝通學(xué)習(xí) ????
UIButton+Button.h
#import <UIKit/UIKit.h>
@interface UIButton (Button)
/**
字體大小
*/
-(UIButton *(^)(CGFloat ))butTexFont;
/**
默認(rèn)文字
*/
-(UIButton *(^)(NSString *))normalTitle;
/**
點(diǎn)擊文字
*/
-(UIButton *(^)(NSString *))selectedTitle;
/**
默認(rèn)背景顏色
*/
-(UIButton *(^)(UIColor *))normalBackgroundColor;
/**
點(diǎn)擊背景顏色
*/
-(UIButton *(^)(UIColor *))selectedBackgroundColor;
/**
默認(rèn)文字顏色
*/
-(UIButton *(^)(UIColor *))normalTitleColor;
/**
點(diǎn)擊文字顏色
*/
-(UIButton *(^)(UIColor *))selectedTitleColor;
/**
默認(rèn)image
*/
-(UIButton *(^)(NSString *))normalImage;
/**
點(diǎn)擊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 --默認(rèn)文字
-(UIButton *(^)(NSString *))normalTitle{
return ^id(NSString * normalTitle){
[self setTitle:normalTitle forState:UIControlStateNormal];
return self;
};
}
#pragma mark ---點(diǎn)擊文字
-(UIButton *(^)(NSString *))selectedTitle{
return ^id(NSString * selectedTitle){
[self setTitle:selectedTitle forState:UIControlStateSelected];
return self;
};
}
#pragma mark --默認(rèn)image
-(UIButton *(^)(NSString *))normalImage{
return ^id(NSString * normalImage){
[self setImage:[UIImage imageNamed:normalImage] forState:UIControlStateNormal];
return self;
};
}
#pragma mark ---點(diǎn)擊image
-(UIButton *(^)(NSString *))selectedImage{
return ^id(NSString * selectedImage){
[self setImage:[UIImage imageNamed:selectedImage] forState:UIControlStateSelected];
return self;
};
}
#pragma mark --默認(rèn)背景顏色
-(UIButton *(^)(UIColor *))normalBackgroundColor{
return ^id(UIColor * normalBackgroundColor){
[self setBackgroundColor:normalBackgroundColor forState:UIControlStateNormal];
return self;
};
}
#pragma mark --點(diǎn)擊背景顏色
-(UIButton *(^)(UIColor *))selectedBackgroundColor{
return ^id(UIColor * selectedBackgroundColor){
[self setBackgroundColor:selectedBackgroundColor forState:UIControlStateSelected];
return self;
};
}
#pragma mark ---默認(rèn)文字顏色
-(UIButton *(^)(UIColor *))normalTitleColor{
return ^id(UIColor * normalTitleColor){
[self setTitleColor:normalTitleColor forState:UIControlStateNormal];
return self;
};
}
#pragma mark ---點(diǎn)擊文字顏色
-(UIButton *(^)(UIColor *))selectedTitleColor{
return ^id(UIColor * selectedTitleColor){
[self setTitleColor:selectedTitleColor forState:UIControlStateSelected];
return self;
};
}
@end