iOS UI控件便利构造器 包含UILabel/UIButton
KKInitializer 简介
常用UI控件的便利构造方法集合 现支持 UILabel/UIButton/UIImage/UITextField/UITextView/UIColor等
一般页面中包含很多小的UI控件,UILabel、UIButton、UIImageView、UIImage、UITextField、UITextView等, 如果用纯代码写UI控件, 系统提供的设置构造方法比较分散,不够简洁效率低,通常我们会写一些便利构造方法,来优化代码提高效率。
本文集成了常用UI控件的便利构造方法,对于一些常用属性,还用链式语法重写了一遍, 让我们代码可以变的更优雅一些。
常用控件用Cocoapods来管理, 方便自己和他人使用。
Cocoapods
全部控件构造方法扩展(推荐):
pod 'KKInitializer'
需要其中1个或多个 ,选择使用:
pod 'KKInitializer/UILabel+KKInitializer'
pod 'KKInitializer/UIButton+KKInitializer'
pod 'KKInitializer/UIImage+KKInitializer'
pod 'KKInitializer/UITextField+KKInitializer'
pod 'KKInitializer/UITextView+KKInitializer'
pod 'KKInitializer/UIColor+KKInitializer'
代码示例
UILabel *label = [UILabel k_labelWithText:@"Label构造器" boldFontSize:15 textColor:UIColor.redColor];
label.k_cornerRadius(5).k_backgroudColor(UIColor.lightGrayColor).k_frame(CGRectMake(100, 100, 100, 40));
[self.view addSubview:label];
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))];
UIImage *img = UIImage.k_imgFillColor(UIColor.redColor, CGSizeMake(100, 100)).k_cornerRadius(5.0f);
UIImageView *imgView = [[UIImageView alloc] initWithImage:img];
imgView.frame = CGRectMake(100, 400, 100, 100);
[self.view addSubview:imgView];
UITextField *textField = UITextField.k_initFrame(CGRectMake(100, 100, 200, 40));
textField.k_placeholder(@"占位占位占位").k_borderStyle(UITextBorderStyleRoundedRect).k_textColor(UIColor.redColor).k_fontSize(20.f);
[self.view addSubview:textField];
UITextView *textView = UITextView.k_init().k_textColor(UIColor.redColor).k_boldFontSize(15.f);
[self.view addSubview:textView];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
view.backgroundColor = UIColor.k_hexColor(0xFF3300);
view.backgroundColor = UIColor.k_RGBColor(255,121,0);
view.backgroundColor = UIColor.k_randomColor();
[self.view addSubview:view];
UILabel *titleLabel = UILabel.k_init();
UILabel *descLabel = UILabel.k_init();
titleLabel.k_textColor(UIColor.k_t1Color);
descLabel.k_textColor(UIColor.k_t2Color);
相关链接
Github: https://github.com/cocoZ/KKInitializer
UILabel+KKInitializer: https://www.jianshu.com/p/4f75be2e21dc
UIButton+KKInitializer: https://www.jianshu.com/p/7fbb5b22a68c
UIImage+KKInitializer: https://www.jianshu.com/p/695957e139e5
UITextField+KKInitializer: https://www.jianshu.com/p/f08757ca52be
UITextView+KKInitializer: https://www.jianshu.com/p/48dda0eb0bb9
UIColor+KKInitializer: https://www.jianshu.com/p/26a69d03c0e4