简化常用控件初始化代码
工厂模式
工厂模式?
利用OC语言的特性“多态”来创建不同的对象
类工厂方法的实现是为了向客户提供方便,它们将分配和初始化合在一个步骤中,返回被创建的对象,并 进行自动释放处理。这些方法的形式是+ (type)className...(其中 className不包括任何前缀)。 工厂方法可能不仅仅为了方便使用。它们不但可以将分配和初始化合在一起,还可以为初始化过程提供对 象的分配信息
常用控件利用工厂模式整合,需要什么类型就创建输出什么类型,简化代码如下:⬇️
MyControl.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface MyControl : NSObject
//工厂模式:传入参数,出来控件
#pragma mark 创建View
+(UIView*)createViewWithFrame:(CGRect)frame;
#pragma mark 创建label
+(UILabel*)createLabelWithFrame:(CGRect)frame Font:(float)font Text:(NSString*)text;
#pragma mark 创建button
+(UIButton*)createButtonWithFrame:(CGRect)frame target:(id)target SEL:(SEL)method title:(NSString*)title;
#pragma mark 创建imageView
+(UIImageView*)createImageViewFrame:(CGRect)frame imageName:(NSString*)imageName;
#pragma mark 创建textField
+(UITextField*)createTextFieldFrame:(CGRect)frame Font:(float)font textColor:(UIColor*)color leftImageName:(NSString*)leftImageName rightImageName:(NSString*)rightImageName bgImageName:(NSString*)bgImageName placeHolder:(NSString*)placeHolder sucureTextEntry:(BOOL)isOpen;
@end
MyControl.m
#import "MyControl.h"
@implementation MyControl
#pragma mark 创建View
+(UIView*)createViewWithFrame:(CGRect)frame
{
UIView*view=[[UIView alloc]initWithFrame:frame];
return view;
}
#pragma mark 创建label
+(UILabel*)createLabelWithFrame:(CGRect)frame Font:(float)font Text:(NSString*)text
{
UILabel*label=[[UILabel alloc]initWithFrame:frame];
//设置字体
label.font=[UIFont systemFontOfSize:font];
//设置折行方式 NSLineBreakByWordWrapping是按照单词折行
label.lineBreakMode=NSLineBreakByWordWrapping;
//折行限制 0时候是不限制行数
label.numberOfLines=0;
//对齐方式
label.textAlignment=NSTextAlignmentLeft;
//设置背景颜色
label.backgroundColor=[UIColor clearColor];
//设置文字
label.text=text;
//自适应
//label.adjustsFontSizeToFitWidth=YES;
return label;
}
#pragma mark 创建button
+(UIButton*)createButtonWithFrame:(CGRect)frame target:(id)target SEL:(SEL)method title:(NSString*)title
{
UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:title forState:UIControlStateNormal];
[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
button.frame=frame;
[button addTarget:target action:method forControlEvents:UIControlEventTouchUpInside];
return button;
}
#pragma mark 创建imageView
+(UIImageView*)createImageViewFrame:(CGRect)frame imageName:(NSString*)imageName{
UIImageView*imageView=[[UIImageView alloc]initWithFrame:frame];
imageView.image=[UIImage imageNamed:imageName];
//用户交互
imageView.userInteractionEnabled=YES;
return imageView;
}
#pragma mark 创建textField
+(UITextField*)createTextFieldFrame:(CGRect)frame Font:(float)font textColor:(UIColor*)color leftImageName:(NSString*)leftImageName rightImageName:(NSString*)rightImageName bgImageName:(NSString*)bgImageName
{
UITextField*textField=[[UITextField alloc]initWithFrame:frame];
textField.font=[UIFont systemFontOfSize:font];
textField.textColor=color;
//左边的图片
UIImage*image=[UIImage imageNamed:leftImageName];
UIImageView*letfImageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
textField.leftView=letfImageView;
textField.leftViewMode=UITextFieldViewModeAlways;
//右边的图片
UIImage*rightImage=[UIImage imageNamed:rightImageName];
UIImageView*rightImageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, rightImage.size.width, rightImage.size.height)];
textField.rightView=rightImageView;
textField.rightViewMode=UITextFieldViewModeAlways;
//清除按钮
textField.clearButtonMode=YES;
//当再次编辑时候清除
//textField.clearsOnBeginEditing=YES;
// //密码遮掩
// textField.secureTextEntry;
// //提示框
// textField.placeholder
return textField;
}
//适配器 为了适配以前的版本,和现有已经开发的所有功能模块,在原有功能模块基础上进行扩展的方式
+(UITextField*)createTextFieldFrame:(CGRect)frame Font:(float)font textColor:(UIColor*)color leftImageName:(NSString*)leftImageName rightImageName:(NSString*)rightImageName bgImageName:(NSString*)bgImageName placeHolder:(NSString*)placeHolder sucureTextEntry:(BOOL)isOpen
{
UITextField*textField= [MyControl createTextFieldFrame:frame Font:font textColor:color leftImageName:leftImageName rightImageName:rightImageName bgImageName:bgImageName];
//适配器扩展出来的方法
textField.placeholder=placeHolder;
textField.secureTextEntry=isOpen;
return textField;
}
@end
使用示例:
UIButton * button04 = [MyControl createButtonWithFrame:CGRectMake(100, 370, 44, 44) target:self SEL:nil title:@"💗" ];
[self.view addSubview:button04];
我是写代码的凡,如有错误,欢迎指正!🙂🙂🙂