iOS开发-UITextField

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。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值