IOS中文本框输入自动隐藏和自动显示

时间:2021-07-08 15:31:19

uilabe和UIText扩展方法

+(UILabel*)LabWithFrame:(CGRect)_rect
text:(NSString*)aText
textColor:(UIColor*)aColor
textAlign:(NSTextAlignment)aAlign
font:(UIFont*)aFont{
UILabel *lab = [[[UILabel alloc] initWithFrame:_rect] autorelease];
lab.backgroundColor = [UIColor clearColor];
if ([aText length] > )
lab.text = aText;
if (aColor)
lab.textColor = aColor;
if(aAlign)
lab.textAlignment = aAlign;
if (aFont)
lab.font = aFont;
return lab;
}
@end
@implementation UITextField (my)
+(UITextField*)TextFieldWithFrame:(CGRect)_rect
target:(id)target
text:(NSString*)aText
textColor:(UIColor*)aTextColor
textAlign:(NSTextAlignment)aAlign
placeHolder:(NSString*)holder
clearMode:(UITextFieldViewMode)aViewMode
{
UITextField *textField = [[[UITextField alloc] initWithFrame:_rect] autorelease];
textField.backgroundColor = [UIColor clearColor];
textField.delegate = target; if (aAlign)
textField.textAlignment = aAlign;
if ([aText length] > )
textField.text = aText;
if (aTextColor)
textField.textColor = aTextColor;
if (aViewMode)
textField.clearButtonMode = aViewMode;
if ([holder length] > )
textField.placeholder = holder;
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; return textField;
}

在viewdidLoad中初始化控件,并且把label放入到集合中

#import "UILabel+my.h"
#import "UITextField+my.h" #define KPadding 10
#define KLbWidth 100
#define KlbHeight 50
#define KFont 12
#define kTextHeight 30
@interface ViewController ()
{
UITextField *UserNameField;
UITextField *PassWordField;
UITextField *yzmField;
UILabel *lb;
}
//存放文本框的集合
@property(nonatomic,retain)NSMutableArray *UIElementLabArray;
//存放文本的标题
@property(nonatomic,retain)NSMutableArray *UIELementLabTitleArray;
//初始化存放所有uilabe的集合。
@property(nonatomic,retain)NSMutableArray *UIElementArray; @end
-(void)InitController{
UserNameField=[UITextField TextFieldWithFrame:CGRectMake(, , , ) target:nil text:@"" textColor:[UIColor blackColor] textAlign:NSTextAlignmentLeft placeHolder:@"" clearMode:UITextFieldViewModeWhileEditing];
UserNameField.borderStyle=UITextBorderStyleRoundedRect;
UserNameField.keyboardType=UIKeyboardTypeNumberPad;
[self.view addSubview:UserNameField];
PassWordField=[UITextField TextFieldWithFrame:CGRectMake(, , , ) target:nil text:@"" textColor:[UIColor blackColor] textAlign:NSTextAlignmentLeft placeHolder:@"" clearMode:UITextFieldViewModeWhileEditing];
PassWordField.borderStyle=UITextBorderStyleRoundedRect;
[self.view addSubview:PassWordField];
yzmField=[UITextField TextFieldWithFrame:CGRectMake(, , , ) target:nil text:@"" textColor:[UIColor blackColor] textAlign:NSTextAlignmentLeft placeHolder:@"" clearMode:UITextFieldViewModeWhileEditing];
yzmField.borderStyle=UITextBorderStyleRoundedRect;
[self.view addSubview:yzmField]; for (int i=; i<; i++) {
UILabel *lb1=[UILabel LabWithFrame:CGRectZero text:@"" textColor:[UIColor blackColor] textAlign:NSTextAlignmentRight font:[UIFont systemFontOfSize:KFont]];
[self.view addSubview:lb1];
[self.UIElementLabArray addObject:lb1];
} }

- (UIImage *) ImageWithColor: (UIColor *) color frame:(CGRect)aFrame
{
UIGraphicsBeginImageContext(aFrame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, aFrame); UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return theImage;
} - (void)viewDidLoad
{
[super viewDidLoad]; UIImage *image=[self ImageWithColor:[UIColor colorWithRed:/255.0f green:/255.0f blue:/255.0f alpha:] frame:self.view.bounds]; image= [image stretchableImageWithLeftCapWidth:image.size.width*0.5f topCapHeight:image.size.height*0.5f];
UIImageView *imageview=[[UIImageView alloc] initWithImage:image];
[self.view addSubview:imageview]; self.UIElementLabArray=[NSMutableArray array]; CGRect bRect=UIEdgeInsetsInsetRect(self.view.bounds, UIEdgeInsetsMake(KPadding, , KPadding, )); //每一行
CGRect rowRect;
//开始分割每一行
CGRectDivide(bRect, &rowRect, &bRect, KlbHeight, CGRectMinYEdge);
CGRect leftRect,rightRect; //开始分割每一列
CGRectDivide(rowRect, &leftRect, &rightRect, KLbWidth, CGRectMinXEdge); lb=[UILabel LabWithFrame:leftRect text:@"username" textColor:[UIColor blackColor] textAlign:NSTextAlignmentRight font:[UIFont systemFontOfSize:KFont]];
[self.view addSubview:lb]; CGRect txtdiv=UIEdgeInsetsInsetRect(rightRect, UIEdgeInsetsMake(, KPadding, KPadding, ));
UITextField * text=[UITextField TextFieldWithFrame:txtdiv target:self text:@"" textColor:[UIColor blackColor] textAlign:NSTextAlignmentLeft placeHolder:@"input" clearMode:UITextFieldViewModeWhileEditing];
text.borderStyle=UITextBorderStyleRoundedRect;
[self.view addSubview:text]; [self InitController]; }
#pragma mark-UITextField delegate

-(void)textFieldDidEndEditing:(UITextField *)textField{

    //初始化数组
self.UIElementArray=[NSMutableArray array];
self.UIELementLabTitleArray=[NSMutableArray array];
//得到数据
NSArray *data=@[@"UN",@"PD",@"yzm"];
for (int i=;i<data.count;i++) {
NSString *str=data[i];
if([str rangeOfString:@"UN"].location!=NSNotFound){
[self.UIElementArray addObject:UserNameField];//添加文本框的控件
[self.UIELementLabTitleArray addObject:data[i]];//添加对应(lable)的title
}
else if([str rangeOfString:@"PD"].location!=NSNotFound){
[self.UIElementArray addObject:PassWordField];//添加文本框的控件
[self.UIELementLabTitleArray addObject:data[i]];
}
else if([str rangeOfString:@"yzm"].location!=NSNotFound){
[self.UIElementArray addObject:yzmField];//添加文本框的控件
[self.UIELementLabTitleArray addObject:data[i]];
}
}
CGFloat top=lb.bounds.origin.y+lb.bounds.size.height;
CGFloat left=lb.bounds.origin.x;
CGRect rect=UIEdgeInsetsInsetRect(self.view.bounds, UIEdgeInsetsMake(top, left, , ));//开始规划显示控件的Rect
CGRect rowRect; [UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
for (int i=; i<self.UIElementArray.count; i++) { //开始分割每一行,brect每一行的高度
CGRectDivide(rect, &rowRect, &rect,KlbHeight, CGRectMinYEdge); CGRect leftRect,rightRect;//leftRect代表label的rect,right代表textField的rect
//开始分割行中列
CGRectDivide(rowRect, &leftRect, &rightRect, KLbWidth, CGRectMinXEdge);
UILabel *lbt=self.UIElementLabArray[i];
lbt.text=self.UIELementLabTitleArray[i];
lbt.frame=leftRect; //设置每一行的高度
CGRect txtRect=UIEdgeInsetsInsetRect(rightRect, UIEdgeInsetsMake(, KPadding, , KPadding));
UITextField *txt=self.UIElementArray[i];
txt.frame=txtRect; }
[UIView commitAnimations]; [UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:]; [UIView commitAnimations];
} -(BOOL)textFieldShouldClear:(UITextField *)textField{
[UIView animateWithDuration:0.3 animations:^{
for(UILabel *label in self.UIElementLabArray){
label.frame=CGRectZero;
}
for (UITextField *txt in self.UIElementArray) {
txt.frame=CGRectMake(, , ,);
}
}]; return YES;
}