程序员iOS Developer

MNKit - 业务开发中简化属性设置的工具类

2018-06-14  本文已影响28人  小蠢驴打代码
made in 小蠢驴的封面

背景:目前的iOS环境,相信大部分的人都还是做的业务开发,一个好的工具类,可以极大的提供开发效率,简化繁琐的设置步骤。接下来,简单介绍一个我在实际开发中抽取出来的工具类 - MNKit

MNButton

业务开发中,UIButton控件应该算最常用的控件之一了,而且它的属性设置还贼麻烦,很多个都是要用 [ set XXX];方法去设置,设置事件(比如点击事件)还要传多个参数- (void)addTarget:(nullable id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents; 所以接下来讲下这个控件怎么设置才能简便使用

//常见方法:
UIButton *btn = [[UIButton alloc]init];
[btn setTitle:@"获取验证码" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
[btn setBackgroundColor:[UIColor lightGrayColor]];
btn.titleLabel.font = [UIFont systemFontOfSize:14];
[self.view addSubview:btn];
[btn addTarget:self action:@selector(clickSendBtn) forControlEvents:UIControlEventTouchUpInside];

上述代码是UIButton的常见创建方式,设置按钮的标题、字体大小、颜色、背景色、点击事件、添加到父控件等等,基本上每个属性都是要通过[ ] 设置,最麻烦的是经常要通过forState:UIControlStateXXX设置状态

//一句代码设置 - 按钮标题 && 颜色 && 字号 && 父试图 && 响应方法
MNButton *sendBtn = [MNButton buttonWithTitle:@"获取验证码"
                                   titleColor:[UIColor orangeColor]
                                     fontSize:[UIFont systemFontOfSize:14]
                              backgroundColor:[UIColor lightGrayColor]
                                   parentView:self.view
                                   targetName:@selector(clickSendBtn)
                                     delegate:self];

这里基本上,button的属性一次性设置就行了,包括target默认是UIControlEventTouchUpInside(点击),传入target的名称就行,这里要切记传的是SEL对象,记得加@selector(xxxName),有了这步设置,这一行代码即可创建拥有上面代码所需要的按钮的基本属性

//一句代码设置 - 按钮背景图片(默认状态) && 父试图 && 响应方法
UIImage *starImage = [UIImage imageNamed:@"Notcollection"];
UIButton *starBtn = [MNButton buttonWithBackgroundImage:starImage
                                             parentView:self.view
                                             targetName:@selector(p_clickStarBtn)
                                               delegate:self];

因为常用的Button设置图片是设置BackgroundImage让他铺满整个button,所以这里的方法传入的属性是BackgroundImage


MNLabel

业务开发中,UIButton控件如果登场率如果不能排在第一,那么比它更常见的非UILabel莫属了,和UIButton一样,UILabel很常见,又经常有许多属性需要设置 - 字体大小、颜色、内容等等

//快速设置内容,文字颜色,字体大小,父控件
[MNLabel mn_labelWithTitle:@"test-label1"
                      font:[UIFont systemFontOfSize:14]
                     color:[UIColor lightGrayColor]
                parentView:self.view];
//设置带行间距的label 
NSString *title2 = @"测试-我是标题2,rua~~~测试-我是标题2,rua~~~测试-我是标题2,rua~~~测试-我是标题2,rua~~~测试-我是标题2,rua~~~测试-我是标题2,rua~~~测试-我是标题2,rua~~~";
CGFloat label2Width = 300;
MNLabel *label2 = [MNLabel mn_labelWithTitle:title2
                                        font:[UIFont systemFontOfSize:14]
                                       color:[UIColor orangeColor]
                                 lineSpacing:4
                                  parentView:self.view];

类似于MNButton,MNLabel也是封装了UILabel的多个属性设置,传入需要设置的这些属性的一句代码即可实现Label所需的多个属性设置(具体其他用法详见Demo)


MNSVProgressClass

这个类是根据项目需求,对SVProgressHUD进行二次封装的,设置完默认的主题(文本颜色、背景色、显隐动画。。。)之后,一行代码即可调用

默认主题设置:

//基础设置 && 多少秒后隐藏
+  (void)setSVDuration:(CGFloat)time{
    
    //设置标题颜色
    [SVProgressHUD setForegroundColor:[UIColor whiteColor]];
    
    //设置背景色
    [SVProgressHUD setBackgroundColor:[UIColor darkGrayColor]];
    
    //n秒后消失
    [SVProgressHUD dismissWithDelay:time];
    
    //消失动画(1S)
    [SVProgressHUD setFadeOutAnimationDuration:1.0];

    //如果响应时间>3s 禁止用户交互
    if(time >= 3){
        [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    }else{
        [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeNone];
    }
}

MNSVProgressClass中,所有带‘number s’方法名的,都是该控件会在number秒后自动消失,如果没带时间的,就是要手动设置[SVProgress dismiss]


因为有一些我封装的工具类可能就我们现在这种项目中用得到,如果有需要或者以后迭代过程中可以通过外部参数控制的,我会再继续抽出来,添加到此工具类中,所以如果觉得MNKit对你有用的,能提升你工作效率的欢迎star~ 后期会继续补充~

Demo

上一篇 下一篇

猜你喜欢

热点阅读