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;
}
- iOS 常用控件的封装以及调用
- iOS常用的封装方法
- iOS封装常用的方法
- 动态调用用dll封装的控件
- KSoap2-android的调用以及封装使用
- 封装iOS自定义控件的体会
- iOS自定义控件的封装细节
- ios 对于常用的NSUserDefults的封装
- IOS常用的UI控件
- ios常用控件的初始化
- iOS 常用控件的使用方法
- iOS 常用控件的创建
- iOS开发常用方法的封装
- ios-封装日期控件
- iOS自己封装控件
- iOS常用封装方法
- IOS 常用方法封装
- WebView控件中常用的调用函数
- Daily 排序之冒泡、选择、插入
- 第十五周项目一(验证算法)
- spring aop之对象内部方法间的嵌套失效
- 第十三周 项目二 最小生成树的克鲁斯卡尔算法
- iOS组件化思路-大神博客研读和思考
- iOS 常用控件的封装以及调用
- Codeforces 49E
- Altium Designer的几种常用布线技巧
- android数据库更新简介
- @RequestMapping 用法详解之地址映射(转)
- tomcat启动很慢很慢,长达122873ms
- 内存溢出的几种可能
- 工作学习记录--angularjs 中的$watch与$observe监听区别
- 第十四周项目1(3)——验证二叉排序树相关算法