UI   控件 —UITextFile

 
(1)可以根据需要设置文本框的样式(包括形状、边框颜色、背景等)。
 
(2)可以根据需要设置文字显示样式(包括输入密码时的密文显示、文字横向居中、纵向居中上下、输入的文字是否首席木大写、文字超过后是否缩小还是向右滚动等)。
 
(3)可以根据需要设置各种不同的键盘样式(只有数字、只有字母等等)。
 
(4)还有inputView可以弹出一个视图,用于取代弹出键盘,暂时不知道什么用处,但貌似可以用得地方很多啊。
 
(5)还有return的样式设置,可以设置为Google也可以设置为Go和Search等更形象的按钮。
 
(6)还有一个clearsOnBeginEditing是否设置清除按钮也很常用。
 
(7)还有用得比较多得估计是左右视图,也就是我们常见的用户名和密码的前面还有一个小icon图片表示用户的“小人”和表示密码的“锁”的图片,用左右视图可以加载进来,当然最后要记得设置左右视图模式为Always,不然默认是Never不显示的。
[objc] view plaincopy
 
  1. - (void)viewDidLoad {  
  2.     //textfiled1本想是textField1的,但不影响  
  3.     UITextField *textFiled1=[[UITextField alloc]init];  
  4.     //此时textField1已存在,但因为是透明背景,所以看不见,但是点击那块地方会发现光标闪烁可写  
  5.     //为了证明是透明背景而不是白色背景,我们可以设置self.view背景为红色,看看textField1是白色还是透明色  
  6. //    self.view.backgroundColor=[UIColor redColor];  
  7.     textFiled1.frame=CGRectMake(10, 30, 300, 30);  
  8.     //设置边框样式  
  9.     //UITextBorderStyleRoundedRect-圆角矩形,背景是白色,不再是透明的  
  10.     //UITextBorderStyleLine-矩形,黑色边框,透明背景  
  11.     //UITextBorderStyleBezel-和上面类似,但是是灰色的边框,背景透明  
  12.     textFiled1.borderStyle=UITextBorderStyleRoundedRect;  
  13.     //设置背景颜色,会覆盖上面圆角矩形默认的白色背景  
  14.     textFiled1.backgroundColor=[UIColor purpleColor];  
  15.     //设置提示(默认)文字  
  16.     textFiled1.placeholder=@"请输入您的密码";  
  17.     //设置密文输入,就是和输入密码时类似的显示为小圆点  
  18.     textFiled1.secureTextEntry=YES;  
  19.     //设置键盘样式,比如银行取款密码只需要数字,有的输入邮箱需要@等等  
  20.     //UIKeyboardTypeAlphabet和UIKeyboardTypeDefault类似,就是我们平时看到那样,都是字母,然后有个按键可以切换符号  
  21.     //UIKeyboardTypeASCIICapable好像和上面差不多  
  22.     //UIKeyboardTypeDecimalPad,UIKeyboardTypeNumberPad都是数字,但前者多了一个“小数点”按键  
  23.     //UIKeyboardTypeEmailAddress-除了字母还有小数点和@出现  
  24.     //UIKeyboardTypeNamePhonePad-貌似正常  
  25.     //UIKeyboardTypePhonePad-电话键盘,不仅有数字还有*和#的那种  
  26.     //UIKeyboardTypeNumbersAndPunctuation-只有数字和标点符号  
  27.     //UIKeyboardTypeTwitter-除了字母还有@和#,这是微博的符号  
  28.     //UIKeyboardTypeURL-除字母,还有.com按钮,方便输入  
  29.     //UIKeyboardTypeWebSearch-主要区别在于return键变成了GO键  
  30.     //注意:如果是最xcode6下的模拟器的话,默认是不调出软键盘的,按CMD+K可以调出,或者在菜单Hardware里地Keyboard里设置  
  31.     textFiled1.keyboardType=UIKeyboardTypeWebSearch;  
  32.     //设置键盘外观  
  33.     //UIKeyboardAppearanceDark和UIKeyboardAppearanceAlert都是把键盘背景变成半透明灰色区别不明显  
  34.     //UIKeyboardAppearanceLight貌似和UIKeyboardAppearanceDefault一样,没啥区别  
  35.     textFiled1.keyboardAppearance=UIKeyboardAppearanceAlert;  
  36.       
  37.     //设置弹出视图,inputView即弹出的不是键盘而是这个视图  
  38.     //设置的frame时,只有高度有用,其他x和y和宽都是无效的,宽是默认的整个键盘宽度  
  39.     UIImageView *imgView1=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"logo-60@3x.png"]];  
  40.     imgView1.frame=CGRectMake(60, 60, 300, 300);  
  41.     textFiled1.inputView=imgView1;  
  42.       
  43.     //设置左视图,就是用户名和密码,有时候放个图片的位置  
  44.     UIView *view1=[[UIView alloc]init];  
  45.     //x和y无效,x都是0,而y是根据高度来自动调整的。即高度如果超过textField则默认是textField高,如小于textField高度,则上下居中显示。唯一有效的就是宽度  
  46.     view1.frame=CGRectMake(10, 500, 50, 10);  
  47.     view1.backgroundColor=[UIColor orangeColor];  
  48.     textFiled1.leftView=view1;  
  49.     //最重要的时:默认它是不显示的即UITextFieldViewModeNever,我们可以设置永远显示UITextFieldViewModeAlways  
  50.     //UITextFieldViewModeUnlessEditing-一开始就有,点击框,呃,貌似还有  
  51.     //UITextFieldViewModeWhileEditing-一开始没有,点击框就出现  
  52.     textFiled1.leftViewMode=UITextFieldViewModeAlways;  
  53.       
  54.     //同样,我们可以设置右视图,当然也可以加载和图片进来  
  55.     UIImageView *imgView2=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"logo-60@3x.png"]];  
  56.     imgView2.frame=CGRectMake(10, 500, 50, 10);  
  57.     textFiled1.rightView=imgView2;  
  58.     textFiled1.rightViewMode=UITextFieldViewModeAlways;  
  59.       
  60.     //设置清除按钮,就是那个叉叉X,一点击整个输入框的文字全部删除重新输入的那个X(我们先注释掉不让右视图显示,来查看效果)  
  61.     //其实我们在写clearButtonMode是它又提示说这是一个UITextFieldViewMode类型,所以也是和上面一样  
  62.     textFiled1.clearButtonMode=UITextFieldViewModeWhileEditing;  
  63.       
  64.     //再次编辑时是否清空内容,这个除特定场景外很少用,会让用户抓狂的  
  65.     //当然为了模拟再次编辑,我们需要鼠标点到其他地方然后再点回来,所以再创建一个textField  
  66.     textFiled1.clearsOnBeginEditing=NO;  
  67.     //这个clearsOnInsertion貌似点击回去再次编辑时不清楚,但是只要一输入内容就会清除之前的  
  68.     textFiled1.clearsOnInsertion=YES;  
  69.     UITextField *textField2=[[UITextField alloc]init];  
  70.     textField2.frame=CGRectMake(10, 80, 300, 100);  
  71.     textField2.borderStyle=UITextBorderStyleRoundedRect;  
  72.     [self.view addSubview:textField2];  
  73.       
  74.     //我们用上面创建的textField2来做如下  
  75.     //纵向对齐方式,默认是居中  
  76.     //UIControlContentVerticalAlignmentCenter居中,所以Top、Bottom就是居上居下。Fill貌似和Top差不多  
  77.     textField2.contentVerticalAlignment=UIControlContentVerticalAlignmentFill;  
  78.     //当然还有横向对齐  
  79.     //也有左中右和Fill四种,但是貌似没看到什么效果,可能对文字无效,因为有专门的针对文字的设置  
  80.     textField2.contentHorizontalAlignment=UIControlContentHorizontalAlignmentRight;  
  81.     //设置文字对齐方式  
  82.     //同样我们输入textAlignment时有提示是NSTextAlignment类型,有好几种,不细讲  
  83.     textField2.textAlignment=NSTextAlignmentCenter;  
  84.     //设置调整文字大小以适配宽度(即输入不下时缩小文字,实在缩小不了了,就向后滚动),默认是向右滚动的  
  85.     textField2.adjustsFontSizeToFitWidth=YES;  
  86.     //设置最小字号,和上面有关,即小于这个字号的时候,我就不缩小了,直接向右滚动  
  87.     textField2.minimumFontSize=2;  
  88.     //设置字母大小样式,输入autocapitalizationType时有提示是UITextAutocapitalizationType类型  
  89.     //UITextAutocapitalizationTypeAllCharacters-所有字母大写(用键盘输入的话发现失效,需要用软键盘输入才有效,以下同理)  
  90.     //UITextAutocapitalizationTypeWords-单词首字母大写  
  91.     //UITextAutocapitalizationTypeSentences-句首字母大写  
  92.     textField2.autocapitalizationType=UITextAutocapitalizationTypeSentences;  
  93.       
  94.     //设置return样式,有Done/Go/Next/Join/Google/Search/Yahoo/EmergencyCall/Send等,除了默认外,其他的按钮都是蓝颜色背景  
  95.     textField2.returnKeyType=UIReturnKeyEmergencyCall;  
  96.       
  97.     [self.view addSubview:textFiled1];  
  98.       
  99.     [super viewDidLoad];  
  100.     // Do any additional setup after loading the view, typically from a nib.  
  101. }  
 
 
 
 
 
 
[摘要:1.建立 01.UITextField* myTextField = [[UITextField alloc]initWithFrame:CGRectMake(50, 100, 200, 50)]; 2.设置托付 01.myTextField.delegate = self;//托付类须要恪守UITextFieldDelegate协定 3. 设置属性 扩大属性 UIControl属性] 
 
1.创建
01.UITextField* myTextField = [[UITextField alloc]initWithFrame:CGRectMake(50, 100, 200, 50)];  
 
2.设置委托
01.myTextField.delegate = self;//委托类需要遵守UITextFieldDelegate协议  
 
 

3.设置属性

扩展属性

UIControl属性对UITextField完全可以用,下面的都是UITextField扩展的属性

01.myTextField.textAlignment = UITextAlignmentLeft;//默认就是左对齐,这个是UITextField扩展属性  
 
02.myTextField.borderStyle = UITextBorderStyleBezel;//默认是没有边框,如果使用了自定义的背景图片边框会被忽略掉  
 
03.myTextField.placeholder = @"请在此输入账号";//为空白文本字段绘制一个灰色字符串作为占位符  
 
04.myTextField.clearsOnBeginEditing = YES;//设置为YES当用点触文本字段时,字段内容会被清除  
 
05.myTextField.adjustsFontSizeToFitWidth = YES;//设置为YES时文本会自动缩小以适应文本窗口大小。默认是保持原来大小,而让长文本滚动  
 
06.//myTextField.background = [UIImage imageNamed:@"registBtn"];//可以接受UIImage对象,此项设置则边框失效。  
 
07.myTextField.clearButtonMode = UITextFieldViewModeUnlessEditing;//右边显示的'X'清楚按钮  
 
08.//myTextField.LeftView =  
 
09.//myTextField.leftViewMode =   
 
10.//myTextField.RightView =  
 
11.//myTextField.rightViewMode =    
 
 
4.下列方法在创建一个UITextField的子类时可以重写:borderRectForBounds指定矩形边界textRectForBounds 指定显示文本的边界placeholderRectForBounds指定站位文本的边界editingRectForBounds指定编辑中文本的边界clearButtonRectForBounds指定显示清除按钮的边界leftViewRectForBounds指定显示左附着视图的边界rightViewRectForBounds指定显示右附着视图的边界委托方法。
========================================
01.- (CGRect)clearButtonForBounds:(CGRect)bounds{  
 
02.    return CGRectMake(bounds.origin.x +bounds.size.width-50,   
 
03.                      bounds.origin.y+bounds.size.height-20, 16, 16);  
 
04.}  
========================================
 
01.- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{  
 
02.    //返回一个BOOL值,指定是否循序文本字段开始编辑  
 
03.    return YES;  
 
04.}  
 
========================================
01.- (void)textFieldDidBeginEditing:(UITextField *)textField{  
 
02.    //开始编辑时触发,文本字段将成为first responder  
 
03.}  
 
========================================
01.- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{  
 
02.    //返回BOOL值,指定是否允许文本字段结束编辑,当编辑结束,文本字段会让出first responder  
 
03.    //要想在用户结束编辑时阻止文本字段消失,可以返回NO  
 
04.    //这对一些文本字段必须始终保持活跃状态的程序很有用,比如即时消息  
 
05.    return NO;  
 
06.}  
 
========================================
01.- (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{  
 
02.    //当用户使用自动更正功能,把输入的文字修改为推荐的文字时,就会调用这个方法。  
 
03.    //这对于想要加入撤销选项的应用程序特别有用  
 
04.    //可以跟踪字段内所做的最后一次修改,也可以对所有编辑做日志记录,用作审计用途。     
 
05.    //要防止文字被改变可以返回NO  
 
06.    //这个方法的参数中有一个NSRange对象,指明了被改变文字的位置,建议修改的文本也在其中  
 
07.    return YES;  
 
08.}  
 
========================================
01.- (BOOL)textFieldShouldClear:(UITextField *)textField{  
 
02.    //返回一个BOOL值指明是否允许根据用户请求清除内容  
 
03.    //可以设置在特定条件下才允许清除内容  
 
04.    return YES;  
 
05.}  
 
 
========================================
01.-(BOOL)textFieldShouldReturn:(UITextField *)textField{  
 
02.    //返回一个BOOL值,指明是否允许在按下回车键时结束编辑  
 
03.    //如果允许要调用resignFirstResponder 方法,这回导致结束编辑,而键盘会被收起  
 
04.    [textField resignFirstResponder];//查一下resign这个单词的意思就明白这个方法了  
 
05.    return YES;  
 
06.}  
 
========================================
虚拟键盘挡住UITextField时的解决方法:
RootViewController.h 中:
#import <UIKit/UIKit.h>
 
@interface RootViewController : UIViewController<UITextFieldDelegate> {
 
UITextField *textField1;
UITextField *textField2;
 
}
@property (nonatomic,retain) UITextField *textField1;
@property (nonatomic ,retain) UITextField *textField2;
 
-(IBAction)backgroundTap:(id)sender;
 
@end
RootViewController.m 中:
#import "RootViewController.h"
 
@implementation RootViewController
@synthesize textField1;
@synthesize textField2;
 
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization.
}
return self;
}
*/
 
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
 
UIView *back = [[UIView alloc] initWithFrame:[[UIScreen mainScreen]bounds]];
back.backgroundColor = [UIColor grayColor];
self.view = back;
[back release];
}
*/
 
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
 
UIControl *_back = [[UIControl alloc] initWithFrame:self.view.frame];
_back.backgroundColor = [UIColor grayColor];
self.view = _back;
[_back release];
[(UIControl *)self.view addTarget:self action:@selector(backgroundTap:) forControlEvents:UIControlEventTouchDown];
 
textField1 = [[UITextField alloc] initWithFrame:CGRectMake(20, 300, 200, 30)];
textField1.backgroundColor = [UIColor clearColor];
textField1.borderStyle = UITextBorderStyleRoundedRect;
textField1.textColor = [UIColor redColor];
textField1.delegate = self;
[self.view addSubview:textField1];
 
textField2 = [[UITextField alloc] initWithFrame:CGRectMake(20, 30, 200, 30)];
textField2.backgroundColor = [UIColor clearColor];
textField2.borderStyle = UITextBorderStyleRoundedRect;
textField2.textColor = [UIColor redColor];
textField2.delegate = self;
[self.view addSubview:textField2];
}
 
 
#pragma mark -
#pragma mark 解决虚拟键盘挡住UITextField的方法
- (void)keyboardWillShow:(NSNotification *)noti
//键盘输入的界面调整 
//键盘的高度
float height = 216.0; 
CGRect frame = self.view.frame; 
frame.size = CGSizeMake(frame.size.width, frame.size.height - height); 
[UIView beginAnimations:@"Curl"context:nil];//动画开始 
[UIView setAnimationDuration:0.30]; 
[UIView setAnimationDelegate:self]; 
[self.view setFrame:frame]; 
[UIView commitAnimations];
}
 
 
-(BOOL)textFieldShouldReturn:(UITextField *)textField
// When the user presses return, take focus away from the text field so that the keyboard is dismissed. 
NSTimeInterval animationDuration = 0.30f; 
[UIView beginAnimations:@"ResizeForKeyboard" context:nil]; 
[UIView setAnimationDuration:animationDuration]; 
CGRect rect = CGRectMake(0.0f, 20.0f, self.view.frame.size.width, self.view.frame.size.height); 
self.view.frame = rect;
[UIView commitAnimations];
[textField resignFirstResponder];
return YES; 
}
 
- (void)textFieldDidBeginEditing:(UITextField *)textField
CGRect frame = textField.frame;
int offset = frame.origin.y + 32 - (self.view.frame.size.height - 216.0);//键盘高度216
NSTimeInterval animationDuration = 0.30f; 
[UIView beginAnimations:@"ResizeForKeyBoard" context:nil]; 
[UIView setAnimationDuration:animationDuration];
float width = self.view.frame.size.width; 
float height = self.view.frame.size.height; 
if(offset > 0)
{
CGRect rect = CGRectMake(0.0f, -offset,width,height); 
self.view.frame = rect; 
[UIView commitAnimations]; 
}
 
#pragma mark -
#pragma mark 触摸背景来关闭虚拟键盘
-(IBAction)backgroundTap:(id)sender
{
// When the user presses return, take focus away from the text field so that the keyboard is dismissed. 
NSTimeInterval animationDuration = 0.30f; 
[UIView beginAnimations:@"ResizeForKeyboard" context:nil]; 
[UIView setAnimationDuration:animationDuration]; 
CGRect rect = CGRectMake(0.0f, 20.0f, self.view.frame.size.width, self.view.frame.size.height); 
self.view.frame = rect;
[UIView commitAnimations];
 
 
[textField1 resignFirstResponder];
[textField2 resignFirstResponder];
}
 
#pragma mark -
 
 
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations.
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
 
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
 
 
// Release any cached data, images, etc. that aren't in use.
}
 
 
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
 
 
- (void)dealloc {
[textField1 release];
[textField2 release];
[super dealloc];
}
RootViewController.m 中的backgroundTap:方法,用来实现触摸背景来关闭虚拟键盘。
这个方法用的时候首先把RootViewController上的view改成UIControl,然后通过UIControl的事件UIControlEventTouchDown来触发上面的方法backgroundTap: 。
注意下面的代码:
UIControl *_back = [[UIControl alloc] initWithFrame:self.view.frame];
_back.backgroundColor = [UIColor grayColor];
self.view = _back;
[_back release];
[(UIControl *)self.view addTarget:self action:@selector(backgroundTap:) forControlEvents:UIControlEventTouchDown];
 
 
解决textField被键盘挡住的问题的方法有三个:
- (void)keyboardWillShow:(NSNotification *)noti;//调整虚拟键盘与self.view之间的关系。
-(BOOL)textFieldShouldReturn:(UITextField *)textField;//触摸键盘上的return键时关闭虚拟键盘
- (void)textFieldDidBeginEditing:(UITextField *)textField;//当编辑文本的时候,如果虚拟键盘挡住了textField,整个view就会向上移动。移动范围是一个键盘的高度216。
 
原文地址:https://www.cnblogs.com/Always-LuoHan/p/5267317.html