iOS 利用函数式编程方法创建基础控件

2021-07-21  本文已影响0人  赵哥窟

由于编码的时候大多数时候使用Xib,但是偶尔利用纯代码创建控件的时候,觉得很麻烦,很多属性需要设置,于是在Git上搜索一个开源库FuncControl 觉得还可以,如果您有好用一点的开源库也请分享一下!

创建UILabel

原生写法

UILabel *labelOrigin = [UILabel new];
labelOrigin.frame = CGRectMake(15, 50, self.view.frame.size.width-15, 50);
labelOrigin.font = [UIFont systemFontOfSize:14];
labelOrigin.textColor = [UIColor darkGrayColor];
labelOrigin.text = @"UILabel_FuncChains";
labelOrigin.backgroundColor = [UIColor yellowColor];
[self.view addSubview:labelOrigin];

简化写法

UILabel *label = [UILabel new]
.func_frame(CGRectMake(15, 50, self.view.frame.size.width-30, 50))
.func_font([UIFont systemFontOfSize:18])
.func_textColor([UIColor darkGrayColor])
.func_text(@"UILabel_FuncChains")
.func_backgroundColor([UIColor yellowColor]);
[self.view addSubview:label];
创建UIButton

原生写法

UIButton *buttonOrigin = [[UIButton alloc]init];
    buttonOrigin.frame = CGRectMake(15, 150, self.view.frame.size.width-15, 50);
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.colors = @[(__bridge id)UIColor.redColor.CGColor, (__bridge id)UIColor.blueColor.CGColor];
    gradientLayer.locations = @[@0.0, @1.0];
    gradientLayer.startPoint = CGPointMake(0, 0);
    gradientLayer.endPoint = CGPointMake(1, 0);
    gradientLayer.frame = buttonOrigin.bounds;
    [buttonOrigin.layer addSublayer:gradientLayer];

    buttonOrigin.titleLabel.font = [UIFont systemFontOfSize:14];
    [buttonOrigin setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [buttonOrigin setTitle:@"UIButton_FuncChains" forState:UIControlStateNormal];
    [buttonOrigin addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:buttonOrigin];

简化写法

UIButton *button = UIButton.func_init.func_frame(CGRectMake(15, 150, self.view.frame.size.width-15, 50))
.func_gradientHorizontalColor(UIColor.redColor,UIColor.blueColor)
.func_font([UIFont systemFontOfSize:14])
.func_titleColor([UIColorwhiteColor])
.func_title(@"UIButton_FuncChains")
.func_addTarget_action(self,@selector(buttonClick));
[self.view addSubview:button];
上一篇 下一篇

猜你喜欢

热点阅读