iOS 常用控件的封装以及调用
2016-12-08 10:33
399 查看
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
相关文章推荐
- iOS--常用控件--WebView与js相互调用
- iOS开发之资讯类App常用分类控件的封装与实现(CollectionView+Swift3.0+)
- ASP常用的几个功能模块封装成函数调用(登录验证,状态切换。。) http://www.tsingfeng.com/archives/?article-89.html
- 关于ASP.NET同页面内【用户控件与父页面】以及【用户控件与用户控件】之间方法调用/传值 一点点经验
- j2ee访问oracle数据库封装类以及调用方法
- ASP.NET同页面内【用户控件与父页面】以及【用户控件与用户控件】之间方法调用
- C#调用Media Player 控件播放音乐的常用方法及属性
- 第三方控件如何加载以及调用?
- Java调用ocx控件以及dll
- c# 右键单击 treeview 控件, 显示菜单 以及 从一个 form 调用 另一个form
- C# Windows Api的一些方法 封装 以及 常用参数
- ios 开发,通讯录信息调用常用方法,这个比较全,不用再整理了;
- 关于ASP.NET同页面内【用户控件与父页面】以及【用户控件与用户控件】之间方法调用/传值 一点点经验
- Java常用包以及String类,函数的调用
- 关于ASP.NET同页面内【用户控件与父页面】以及【用户控件与用户控件】之间方法调用/传值 一点点经验
- 关于ASP.NET同页面内【用户控件与父页面】以及【用户控件与用户控件】之间方法调用
- 在atlas里面的UpdatePanel控件中调用javascript,以及updatePanel的一些原理
- 关于ASP.NET同页面内【用户控件与父页面】以及【用户控件与用户控件】之间方法调用/传值 一点点经验
- C# 后台调用前台的JS方法 以及常用的代码片断
- 关于ASP.NET同页面内【用户控件与父页面】以及【用户控件与用户控件】之间方法调用/传值 一点点经验