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

简介


用代码写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;
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 我不忍 打破这夜的宁静 倾听是一种无声的安慰 秒针滴答的声音 在空旷的屋子里时断时续 有时候我误以为那是我下落的...
    老曲同学阅读 1,020评论 4 2
  • 从小一起长大的表哥给我发来微信,他说之前我提到自己手里有些钱能给他应急用的事,现在他的工厂资金周转不开,想借用那些...
    尔东木易阅读 1,560评论 0 0
  • 和男票干了一仗,双方把对方在雨天泥地里撂倒躺平挺尸。幼稚死了!吵架了,我说了想自己一个人静一静而已,妈的搂着我脖子...
    eb00ce51bf07阅读 1,729评论 0 0
  • 原题: https://leetcode.com/problems/add-two-numbers/descrip...
    Azurelore阅读 942评论 0 0