IOS中文本框输入自动隐藏和自动显示
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;
}
IOS中文本框输入自动隐藏和自动显示的更多相关文章
- 使用jquery实现文本框输入特效:文字逐个显示逐个消失反复循环
前两天看到某个网站上的输入框有个小特效:文字逐个显示,并且到字符串最大长度后,逐个消失,然后重新循环显示消失,循环显示字符串数组.我对这个小特效有点好奇,于是今天自己尝试用jquery写一个简单的小d ...
- 转:zTree树控件扩展篇:巧用zTree控件实现文本框输入关键词自动模糊查找zTree树节点实现模糊匹配下拉选择效果
是否可以借助于zTree实现文本框输入关键词自动模糊匹配zTree下拉树,然后选择下拉树内节点显示在文本框内且隐藏下拉树. 看到这个需求脑子里头大致已经想到了要如何实现这样一个需求,当时是限于时间问题 ...
- 转摘:ashx+jquery-autocomplete文本框输入提示功能Asp.net
引入所需文件 <script type="text/javascript" src="JS/jquery-1.8.2.min.js"></sc ...
- 实时监听文本框输入 oninput、onchange与onpropertychange事件的用法和区别
前端页面开发的很多情况下都需要实时监听文本框输入,比如腾讯微博编写140字的微博时输入框hu9i动态显示还可以输入的字数.过去一般都使用onchange/onkeyup/onkeypress/onke ...
- js入门-文本框输入特定内容控制另一个文本框
在填写表单时,有时需要某些文本框隐藏,当一文本框输入特定内容时才会显示隐藏的文本框,这一功能可以用onchange事件或oninput事件实现.下面对比下两种方法实现的区别: onchange()定义 ...
- 监听文本框输入oninput和onpropertychange事件
前端页面开发的很多情况下都需要实时监听文本框输入,比如腾讯微博编写140字的微博时输入框动态显示还可以输入的字数.过去一般都使用onchange/onkeyup/onkeypress/onkeydow ...
- JS和vue文本框输入改变p标签的内容测试
文本框输入,p标签的内容自动变成文本框的内容,如下是三种方法的测试: 方法1:JS里的onchange,当文本框内容改变事件,该事件里写的方法是,获取p标签本身,然后获取文本框的值,赋值给变量,最后给 ...
- 监听文本框输入开发仿新浪微博限制输入字数的textarea插件
监听文本框输入 Firefox.Chrome.IE9,IE10 均支持 oninput 事件,此外所有版本的 IE 均支持 onpropertychange 事件. oninput 事件在用户输入.退 ...
- 【学习】文本框输入监听事件oninput
真实项目中遇到的,需求是:一个文本框,一个按钮,当文本框输入内容时,按钮可用,当删除内容时,按钮不可用. 刚开始用的focus和blur, $(".pay-text").focus ...
随机推荐
- 别再为了this发愁了:JS中的this机制
题记:JavaScript中有很多令人困惑的地方,或者叫做机制.但是,就是这些东西让JavaScript显得那么美好而与众不同.比方说函数也是对象.闭包.原型链继承等等,而这其中就包括颇让人费解的th ...
- 开源ckplayer 网页播放器去logo去广告去水印修改
功能设置介绍 本教程涉及到以下各点,点击对应标题页面将直接滑动到相应内容: 1:修改或去掉播放器前置logo 2:修改或去掉右上角的logo 3:修改.关闭.设置滚动文字广告 4:去掉右边的开关灯分享 ...
- 2017 年度码云新增项目排行榜 TOP 50,为它们打“call”
2017 年度码云新增项目排行榜 TOP 50 正式出炉 !2017 结束了,我们来关注一下这一年里码云上新增的最热门的开源项目吧.此榜单根据 2017 年在码云上新增开源项目的 Watch.Star ...
- C# Winform 未能加载文件或程序集"System.Data.SQLite"或它的某一个依赖项。试图加载格式不正确的程序
在使用Winform 开发了一个小软件,其中使用了SQLite作为数据库 但在我的Win7 64位系统上却出现了以下错误: System.BadImageFormatException: 未能加载文件 ...
- python性能还是不错的
一个未优化的程序,跑了四天,字典长度有15万条,每条40个字段,跑得还算不错. 刚刚优化后的: 等运行两天后再看看效果...
- GIF添加3D加速
由于浏览器内核对Gif格式的图片会产生卡的情况,所以我们需要告诉浏览器,开启一下加速,方法很简单,就是利用css3的特性,强制告诉浏览器,这是个元素,需要3D转换,请务必开启加速效果 方法1 给gif ...
- Redis内存淘汰机制
转自:https://my.oschina.net/andylucc/blog/741965 摘要 Redis是一款优秀的.开源的内存数据库,我在阅读Redis源码实现的过程中,时时刻刻能感受到Red ...
- File /hbase could only be replicated to 0 nodes instead of minReplication (=1). There are 30 datanode(s) running and no node(s) are excluded in this operation.
原因: hdfs-site.xml中的配置为: <property> <name>dfs.datanode.du.reserved</name> <value ...
- 【Linux】Ubuntu配置服务自启动 sysv-rc-conf
在Ubuntu下,配置服务系统开机自启动,使用的不是chkconfig,而是sysv-rc-conf. 且看如下: 安装: sudo apt-get install sysv-rc-conf 帮助信息 ...
- Linux中使用ps、awk、sh一起批量杀死所有的dotnet进程。
一.操作 Linux中使用ps.awk.sh一起批量杀死所有的dotnet进程. 二.参考命令 ps -ef|grep dotnet|awk 'NR==2{print "kill " ...