美文网首页
iOS UIButton便利构造器(初始化方法)

iOS UIButton便利构造器(初始化方法)

作者: 小可无敌斩 | 来源:发表于2019-01-05 13:46 被阅读22次

简介


用代码写UIButton控件,用系统提供的set方法来设置title等属性,代码不够简洁、效率稍低, 这里写了UIButton的category,写了几个简单的构造方法和一些链式语法设置基本属性,来优化代码提高编程效率。

这是一系列UI控件便利构造方法中的一个, 用pod管理,方便自己和他人使用。

Cocoapods


pod 'KKInitializer/UIButton+KKInitializer'

代码示例


 UIButton *btn = [UIButton k_btnForCustomTypeWithTitle:@"Button构造器" titleColor:[UIColor whiteColor] fontSize:20];
 btn.k_bgImgColor([UIColor purpleColor]).k_cornerRadius(5.0f);
 [self.view addSubview:btn.k_frame(CGRectMake(100, 200, 150, 40))];

相关链接


Github: https://github.com/cocoZ/KKInitializer

KKInitializer: https://www.jianshu.com/p/e6ef1df5dd9a

UIButton+KKInitializer接口


+ (instancetype)k_btnForSystomTypeWithTitle:(NSString *)title;
+ (instancetype)k_btnForSystomTypeWithTitle:(NSString *)title titleColor:(UIColor *)titleColor;
+ (instancetype)k_btnForSystomTypeWithTitle:(NSString *)title titleColor:(UIColor *)titleColor fontSize:(CGFloat)fontSize;
+ (instancetype)k_btnForSystomTypeWithTitle:(NSString *)title titleColor:(UIColor *)titleColor boldFontSize:(CGFloat)blodFontSize;

+ (instancetype)k_btnForCustomTypeWithTitle:(NSString *)title;
+ (instancetype)k_btnForCustomTypeWithTitle:(NSString *)title titleColor:(UIColor *)titleColor;
+ (instancetype)k_btnForCustomTypeWithTitle:(NSString *)title titleColor:(UIColor *)titleColor fontSize:(CGFloat)fontSize;
+ (instancetype)k_btnForCustomTypeWithTitle:(NSString *)title titleColor:(UIColor *)titleColor boldFontSize:(CGFloat)boldFontSize;



+ (UIButton *(^)(void))k_init;
+ (UIButton *(^)(UIButtonType type))k_initType;
- (UIButton *(^)(CGRect frame))k_frame;
- (UIButton *(^)(UIFont *font))k_font;
///  设置的是UIView的背景色,无点击效果
- (UIButton *(^)(UIColor *bgColor))k_bgColor;
///  根据颜色生成image, 设置UIButton的backgroundImage, 有点击背景变暗效果
- (UIButton *(^)(UIColor *bgImgColor))k_bgImgColor;
///  设置button圆角
- (UIButton *(^)(CGFloat cornerRadius))k_cornerRadius;

///  state 默认 normal
- (UIButton *(^)(NSString *title))k_title;
- (UIButton *(^)(UIColor *color))k_titleColor;
- (UIButton *(^)(UIImage *img))k_img;
- (UIButton *(^)(UIImage *bgImg))k_bgImg;


- (UIButton *(^)(NSString *title, UIControlState state))k_title_state;
- (UIButton *(^)(UIColor *color, UIControlState state))k_titleColor_state;
- (UIButton *(^)(UIImage *img, UIControlState state))k_img_state;
- (UIButton *(^)(UIImage *bgImg, UIControlState state))k_bgImg_state;

相关文章

网友评论

      本文标题:iOS UIButton便利构造器(初始化方法)

      本文链接:https://www.haomeiwen.com/subject/khvrrqtx.html