iOS 常用控件的封装以及调用

      

     iOS 开发中,无非就是界面搭建以及数据配置 .今天我们就对我们常用控件进行简单的封装.让我们的代码书写更加简洁.耦合性更低!

      如果你想了解并使用 AF 数据请求的简单封装,请参考我的另一篇博客文章iOS 数据请求类AFNetworking 的简单封装.

      大家知道, 我们在项目中对界面的搭建处理常用的 btn.label.image等使用次数只能说很多很多很多!而且书写的时候他们的诸多属性有时候突然想不起来着实让人头疼!这样也显得代码累赘,也让开发过程显得枯燥!这时,我们可以单独封装出来一个控件类,使用中就如同调用系统的一个协议方法那样简单,做到一段代码实现一个控件的效果!

   下面我们来看代码:

   首先,我们还是要创建一个单独的类 ,继承与 NSObject.

   如: 

   .h 文件

#import <Foundation/Foundation.h>

#import <UIKit/UIKit.h>

@interface SL_UI : NSObject


//AlertViewController

//1. alert

+(nullable UIAlertController *)SL_UI_Alert:(nullable UIViewController *)BSelf PromptMassage:(nullable NSString *)prompt titles:(nullable NSString *)titles determine:(nullable void(^)(UIAlertAction  * _Nonnull action))hander ;

///2.sheet ---- > 更新中<暂时不能使用该方法 ... >

+(nullable UIAlertController *)SL_UI_Sheet:(nullable UIViewController *)BSelf PromptMassage:(nullable NSString *)prompt titles:(nullable NSString *)titles determineArr:(nullable NSArray *)arr determine:(nullable void(^)(UIAlertAction  * _Nonnull action))hander ;

//UILabel

+(nullable UILabel *)SL_UI_Label:(nullable NSString *)text color:(nullable UIColor *)textcolor textAlignment:(NSTextAlignment)textAlignment textFont:(NSInteger)sizeNum numberOfLines:(NSInteger)numberOfLines;


//UIButton

+(nullable UIButton *)SL_UI_Btn:(nullable NSString *)title Color:(nullable UIColor *)titleColor Font:( NSInteger)sizeNum bgimage:(nullable UIImage *)image selecteImage:(nullable UIImage *)selecteImage target:(nullable id)target action:( nonnull SEL)action;

//UIImageView

+(nullable UIImageView *)SL_UI_UIimg:(nullable UIImage *)image;

//UITextfield

+(nullable UITextField *)SL_UI_Field:(nullable NSString *)placeholderString font:(NSInteger)sizeNum textAlignment:(NSTextAlignment)textAlignment borderStyle:(UITextBorderStyle)borderStyle clearOnBeginEditing:(BOOL)clear secure:(BOOL)secure keyBoardStyle:(UIKeyboardType)keyBoardStyle;

//导航条 title

+(nullable UILabel *)SL_NavTitle:(nullable NSString *)title;

//导航条BackBtn

+(nullable UIBarButtonItem *)SL_BackBtn:(nullable UIImage *)image target:(nullable id)target action:(nonnull SEL)action;

//提示Label

+(nullable UILabel *)SL_AnimationLabel:(nullable NSString *)string;



   好了,接下来我们到.m 文件中来实现这些方法!


#import "SL_UI.h"


@implementation SL_UI


+(nullable UIAlertController *)SL_UI_Alert:(nullable UIViewController *)BSelf PromptMassage:(nullable NSString *)prompt titles:(nullable NSString *)titles determine:(nullable void(^)(UIAlertAction  * _Nonnull action))hander ;

{

    /*

     typedef NS_ENUM(NSInteger, UIAlertControllerStyle) {

     UIAlertControllerStyleActionSheet = 0,//下铺

     UIAlertControllerStyleAlert //弹出

     }

     */

    UIAlertController * alertView =[UIAlertController alertControllerWithTitle:prompt message:titles preferredStyle:UIAlertControllerStyleAlert];

    /*

     typedef NS_ENUM(NSInteger, UIAlertActionStyle) {

     UIAlertActionStyleDefault = 0,//无状态

     UIAlertActionStyleCancel,//蓝色

     UIAlertActionStyleDestructive//红色

     }

   */

    UIAlertAction * cancel =[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];

    UIAlertAction * determine =[UIAlertAction actionWithTitle:@"Determine" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {

        hander(action);

    }];

    [alertView addAction:cancel];

    [alertView addAction:determine];

    [BSelf presentViewController:alertView animated:YES completion:nil];

    return alertView;

}

//待更新 ....

+(nullable UIAlertController *)SL_UI_Sheet:(nullable UIViewController *)BSelf PromptMassage:(nullable NSString *)prompt titles:(nullable NSString *)titles determineArr:(nullable NSArray *)arr determine:(nullable void(^)(UIAlertAction  * _Nonnull action))hander ;

{

    UIAlertController * alertView =[UIAlertController alertControllerWithTitle:prompt message:titles preferredStyle:UIAlertControllerStyleActionSheet];

    UIAlertAction * cancel =[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];

    

    for (int a = 0; a < arr.count; a ++) {

        

        UIAlertAction * determine =[UIAlertAction actionWithTitle:arr[a] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

           

            hander(action);

        }];


        [alertView addAction:determine];

    }

    

    [alertView addAction:cancel];

    [BSelf presentViewController:alertView animated:YES completion:nil];

    return alertView;


}

+(nullable UILabel *)SL_UI_Label:(nullable NSString *)text color:(nullable UIColor *)textcolor textAlignment:(NSTextAlignment)textAlignment textFont:(NSInteger)sizeNum numberOfLines:(NSInteger)numberOfLines;

{

    UILabel * label =[[UILabel alloc]init];

    label.text=text;

    label.textColor =textcolor;

    label.font=[UIFont systemFontOfSize:sizeNum];

    label.textAlignment =textAlignment;

    label.numberOfLines =numberOfLines;

    

    return label;

}

+(nullable UIButton *)SL_UI_Btn:(nullable NSString *)title Color:(nullable UIColor *)titleColor Font:( NSInteger)sizeNum bgimage:(nullable UIImage *)image selecteImage:(nullable UIImage *)selecteImage target:(nullable id)target action:( nonnull SEL)action;

{

    UIButton * btn =[UIButton buttonWithType:UIButtonTypeCustom];

    [btn setTitle:title forState:UIControlStateNormal];

    [btn setTitleColor:titleColor forState:UIControlStateNormal];

    btn.titleLabel.font =[UIFont systemFontOfSize:sizeNum];

    [btn setBackgroundImage:image forState:UIControlStateNormal];

    [btn setBackgroundImage:selecteImage forState:UIControlStateSelected];

    [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];

    return btn;

}

+(nullable UIImageView *)SL_UI_UIimg:(nullable UIImage *)image;

{

    UIImageView *imageName =[[UIImageView alloc]init];

    imageName.image =image;

    return imageName;

}

+(nullable UITextField *)SL_UI_Field:(nullable NSString *)placeholderString font:(NSInteger)sizeNum textAlignment:(NSTextAlignment)textAlignment borderStyle:(UITextBorderStyle)borderStyle clearOnBeginEditing:(BOOL)clear secure:(BOOL)secure keyBoardStyle:(UIKeyboardType)keyBoardStyle;

{

    UITextField * textField =[[UITextField alloc]init];

    textField.placeholder =placeholderString;

    textField.font =[UIFont boldSystemFontOfSize:sizeNum];

    textField.textAlignment =textAlignment;

    textField.borderStyle =borderStyle;

    textField.clearsOnBeginEditing =clear;

    textField.secureTextEntry =secure;

    textField.keyboardType =keyBoardStyle;

    return textField;

}

+(nullable UILabel *)SL_NavTitle:(nullable NSString *)title;

{

    UILabel * label =[[UILabel alloc]init];

    label.bounds =CGRectMake(0, 0, 100, 25);

    label.text =title;

    label.textColor =[UIColor colorWithRed:255/255 green:255/255 blue:255/255 alpha:1.0];

    label.font =[UIFont systemFontOfSize:16];

    label.numberOfLines =2;

    label.textAlignment =NSTextAlignmentCenter;

    

    return label;

}


+(nullable UIBarButtonItem *)SL_BackBtn:(nullable UIImage *)image target:(nullable id)target action:(nonnull SEL)action;

{

    UIButton * backBtn =[SL_UI SL_UI_Btn:nil Color:nil Font:1 bgimage:image selecteImage:nil target:target action:action];

    backBtn.bounds =CGRectMake(0, 0, 10, 17.42);

    UIBarButtonItem *backItem =[[UIBarButtonItem alloc]initWithCustomView:backBtn];

    

    return backItem;

}

+(nullable UILabel *)SL_AnimationLabel:(nullable NSString *)string;

{

    UILabel * label =[[UILabel alloc]initWithFrame:CGRectMake(DEVICE_WIDTH * 0.1, DEVICE_HEIGHT * 0.88, DEVICE_WIDTH * 0.8, DEVICE_HEIGHT * 0.08)];

    label.text =string;

    label.font =[UIFont systemFontOfSize:14];

    label.backgroundColor =[UIColor blackColor];

    label.textColor =[UIColor whiteColor];

    label.textAlignment =NSTextAlignmentCenter;

    label.layer.cornerRadius =5;

    label.layer.masksToBounds =YES;

    label.numberOfLines =2;

    

    [UIView  animateWithDuration:2 animations:^{

        label.alpha =0;

    } completion:^(BOOL finished) {

        [label removeFromSuperview];

    }];

    

    return label;

}


  //到此,我们常用的控件,就被我们做了一个简单的封装,使用的时候我们只需这样:

-(UILabel *)nameLabel

{

    if (!_nameLabel) {

        self.nameLabel =[SL_UI SL_UI_Label:@"Western restaurant" color:COLOR__(blackColor) textAlignment:NSTextAlignmentLeft textFont:15 numberOfLines:1];

        _nameLabel.frame =UIKIT__FRAME(100, 35, DEVICE_WIDTH * 0.6, 30);

    }

    return _nameLabel;

}


//就轻松的实现了一个 label 的懒加载!并添加了我们想要的属性!
 
      代码整理还不够完善,如果有好的建议,请在下边留言, 如果对文章感兴趣,欢迎关注小白博客或加入公共讨论群:  234713941

 




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值