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

来源:互联网 发布:编程代码有几种 编辑:程序博客网 时间:2024/06/04 22:56
      

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

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

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

   下面我们来看代码:

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

   如: 

   .h 文件

#import <Foundation/Foundation.h>

#import <UIKit/UIKit.h>

@interface SL_UI :NSObject


//AlertViewController

//1. alert

+(nullableUIAlertController *)SL_UI_Alert:(nullableUIViewController *)BSelf PromptMassage:(nullableNSString *)prompt titles:(nullableNSString *)titles determine:(nullablevoid(^)(UIAlertAction  *_Nonnull action))hander ;

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

+(nullableUIAlertController *)SL_UI_Sheet:(nullableUIViewController *)BSelf PromptMassage:(nullableNSString *)prompt titles:(nullableNSString *)titles determineArr:(nullableNSArray *)arr determine:(nullablevoid(^)(UIAlertAction  *_Nonnull action))hander ;

//UILabel

+(nullableUILabel *)SL_UI_Label:(nullableNSString *)text color:(nullableUIColor *)textcolor textAlignment:(NSTextAlignment)textAlignment textFont:(NSInteger)sizeNum numberOfLines:(NSInteger)numberOfLines;


//UIButton

+(nullableUIButton *)SL_UI_Btn:(nullableNSString *)title Color:(nullableUIColor *)titleColor Font:(NSInteger)sizeNum bgimage:(nullableUIImage *)image selecteImage:(nullableUIImage *)selecteImage target:(nullableid)target action:(nonnullSEL)action;

//UIImageView

+(nullableUIImageView *)SL_UI_UIimg:(nullableUIImage *)image;

//UITextfield

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

//导航条 title

+(nullableUILabel *)SL_NavTitle:(nullableNSString *)title;

//导航条BackBtn

+(nullableUIBarButtonItem *)SL_BackBtn:(nullableUIImage *)image target:(nullableid)target action:(nonnullSEL)action;

//提示Label

+(nullableUILabel *)SL_AnimationLabel:(nullableNSString *)string;



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


#import "SL_UI.h"


@implementation SL_UI


+(nullableUIAlertController *)SL_UI_Alert:(nullableUIViewController *)BSelf PromptMassage:(nullableNSString *)prompt titles:(nullableNSString *)titles determine:(nullablevoid(^)(UIAlertAction  *_Nonnull action))hander ;

{

    /*

     typedef NS_ENUM(NSInteger, UIAlertControllerStyle) {

     UIAlertControllerStyleActionSheet = 0,//下铺

     UIAlertControllerStyleAlert //弹出

     }

     */

    UIAlertController * alertView =[UIAlertControlleralertControllerWithTitle:promptmessage:titlespreferredStyle:UIAlertControllerStyleAlert];

    /*

     typedef NS_ENUM(NSInteger, UIAlertActionStyle) {

     UIAlertActionStyleDefault = 0,//无状态

     UIAlertActionStyleCancel,//蓝色

     UIAlertActionStyleDestructive//红色

     }

   */

    UIAlertAction * cancel =[UIAlertActionactionWithTitle:@"Cancel"style:UIAlertActionStyleCancelhandler:nil];

    UIAlertAction * determine =[UIAlertActionactionWithTitle:@"Determine"style:UIAlertActionStyleDestructivehandler:^(UIAlertAction *_Nonnull action) {

        hander(action);

    }];

    [alertView addAction:cancel];

    [alertView addAction:determine];

    [BSelf presentViewController:alertViewanimated:YEScompletion:nil];

    return alertView;

}

//待更新 ....

+(nullableUIAlertController *)SL_UI_Sheet:(nullableUIViewController *)BSelf PromptMassage:(nullableNSString *)prompt titles:(nullableNSString *)titles determineArr:(nullableNSArray *)arr determine:(nullablevoid(^)(UIAlertAction  *_Nonnull action))hander ;

{

    UIAlertController * alertView =[UIAlertControlleralertControllerWithTitle:promptmessage:titlespreferredStyle:UIAlertControllerStyleActionSheet];

    UIAlertAction * cancel =[UIAlertActionactionWithTitle:@"取消"style:UIAlertActionStyleCancelhandler:nil];

    

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

        

        UIAlertAction * determine =[UIAlertActionactionWithTitle:arr[a]style:UIAlertActionStyleDefaulthandler:^(UIAlertAction *_Nonnull action) {

           

            hander(action);

        }];


        [alertView addAction:determine];

    }

    

    [alertView addAction:cancel];

    [BSelf presentViewController:alertViewanimated:YEScompletion:nil];

    return alertView;


}

+(nullableUILabel *)SL_UI_Label:(nullableNSString *)text color:(nullableUIColor *)textcolor textAlignment:(NSTextAlignment)textAlignment textFont:(NSInteger)sizeNum numberOfLines:(NSInteger)numberOfLines;

{

    UILabel * label =[[UILabelalloc]init];

    label.text=text;

    label.textColor =textcolor;

    label.font=[UIFontsystemFontOfSize:sizeNum];

    label.textAlignment =textAlignment;

    label.numberOfLines =numberOfLines;

    

    return label;

}

+(nullableUIButton *)SL_UI_Btn:(nullableNSString *)title Color:(nullableUIColor *)titleColor Font:(NSInteger)sizeNum bgimage:(nullableUIImage *)image selecteImage:(nullableUIImage *)selecteImage target:(nullableid)target action:(nonnullSEL)action;

{

    UIButton * btn =[UIButtonbuttonWithType:UIButtonTypeCustom];

    [btn setTitle:titleforState:UIControlStateNormal];

    [btn setTitleColor:titleColorforState:UIControlStateNormal];

    btn.titleLabel.font =[UIFontsystemFontOfSize:sizeNum];

    [btn setBackgroundImage:imageforState:UIControlStateNormal];

    [btn setBackgroundImage:selecteImageforState:UIControlStateSelected];

    [btn addTarget:targetaction:actionforControlEvents:UIControlEventTouchUpInside];

    return btn;

}

+(nullableUIImageView *)SL_UI_UIimg:(nullableUIImage *)image;

{

    UIImageView *imageName =[[UIImageViewalloc]init];

    imageName.image =image;

    return imageName;

}

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

{

    UITextField * textField =[[UITextFieldalloc]init];

    textField.placeholder =placeholderString;

    textField.font =[UIFontboldSystemFontOfSize:sizeNum];

    textField.textAlignment =textAlignment;

    textField.borderStyle =borderStyle;

    textField.clearsOnBeginEditing =clear;

    textField.secureTextEntry =secure;

    textField.keyboardType =keyBoardStyle;

    return textField;

}

+(nullableUILabel *)SL_NavTitle:(nullableNSString *)title;

{

    UILabel * label =[[UILabelalloc]init];

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

    label.text =title;

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

    label.font =[UIFontsystemFontOfSize:16];

    label.numberOfLines =2;

    label.textAlignment =NSTextAlignmentCenter;

    

    return label;

}


+(nullableUIBarButtonItem *)SL_BackBtn:(nullableUIImage *)image target:(nullableid)target action:(nonnullSEL)action;

{

    UIButton * backBtn =[SL_UISL_UI_Btn:nilColor:nilFont:1bgimage:imageselecteImage:niltarget:targetaction:action];

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

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

    

    return backItem;

}

+(nullableUILabel *)SL_AnimationLabel:(nullableNSString *)string;

{

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

    label.text =string;

    label.font =[UIFontsystemFontOfSize:14];

    label.backgroundColor =[UIColorblackColor];

    label.textColor =[UIColorwhiteColor];

    label.textAlignment =NSTextAlignmentCenter;

    label.layer.cornerRadius =5;

    label.layer.masksToBounds =YES;

    label.numberOfLines =2;

    

    [UIView animateWithDuration:2animations:^{

        label.alpha =0;

    } completion:^(BOOL finished) {

        [labelremoveFromSuperview];

    }];

    

    return label;

}


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

-(UILabel *)nameLabel

{

    if (!_nameLabel) {

        self.nameLabel =[SL_UISL_UI_Label:@"Western restaurant"color:COLOR__(blackColor)textAlignment:NSTextAlignmentLefttextFont:15numberOfLines:1];

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

    }

    return_nameLabel;

}


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

 




0 0