UITextField使用总结

jopen 8年前

UITextField使用总结

UITextField* myTextField = [[UITextField alloc]initWithFrame:CGRectMake(50, 100, 200, 50)];  

myTextField.delegate = self;//委托类需要遵守UITextFieldDelegate协议 

设置属性

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

myTextField.textAlignment = UITextAlignmentLeft;//默认就是左对齐,这个是UITextField扩展属性 

myTextField.borderStyle = UITextBorderStyleBezel;//默认是没有边框,如果使用了自定义的背景图片边框会被忽略掉 

myTextField.placeholder = @"请在此输入账号";//为空白文本字段绘制一个灰色字符串作为占位符 

myTextField.clearsOnBeginEditing = YES;//设置为YES当用点触文本字段时,字段内容会被清除 

myTextField.adjustsFontSizeToFitWidth = YES;//设置为YES时文本会自动缩小以适应文本窗口大小。默认是保持原来大小,而让长文本滚动 

//myTextField.background = [UIImage imageNamed:@"registBtn"];//可以接受UIImage对象,此项设置则边框失效。 

myTextField.clearButtonMode = UITextFieldViewModeUnlessEditing;//右边显示的'X'清楚按钮 

//myTextField.LeftView =  

//myTextField.leftViewMode =  

//myTextField.RightView =  

//myTextField.rightViewMode =  

    

   这些属性令你可以将UIView的派生类附着于为本字段的左方或右方。人们通常会将UIButton对象,比如放大镜或者书签按钮附着与文本字段上。每个附着视图都会有一个相应的模式,设置clearButtonmode属性的那些值,同样可以设置这个模式。

显示

[self.view addSubview:myTextField];


委托方法

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{  

  //返回一个BOOL值,指定是否循序文本字段开始编辑 

  return YES;  


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

  //开始编辑时触发,文本字段将成为first responder  

 

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{  

  //返回BOOL值,指定是否允许文本字段结束编辑,当编辑结束,文本字段会让出first responder  

  //要想在用户结束编辑时阻止文本字段消失,可以返回NO  

  //这对一些文本字段必须始终保持活跃状态的程序很有用,比如即时消息 

  return NO;  

}  

- (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{  

  //当用户使用自动更正功能,把输入的文字修改为推荐的文字时,就会调用这个方法。 

  //这对于想要加入撤销选项的应用程序特别有用  

  //可以跟踪字段内所做的最后一次修改,也可以对所有编辑做日志记录,用作审计用途。   

  //要防止文字被改变可以返回NO  

  //这个方法的参数中有一个NSRange对象,指明了被改变文字的位置,建议修改的文本也在其中 

  //同时在这里是可以做文本长度限制的判断处理的

  return YES;  

}  

- (BOOL)textFieldShouldClear:(UITextField *)textField{  

  //返回一个BOOL值指明是否允许根据用户请求清除内容 

  //可以设置在特定条件下才允许清除内容  

  return YES;  

}  


-(BOOL)textFieldShouldReturn:(UITextField *)textField{  

  //返回一个BOOL值,指明是否允许在按下回车键时结束编辑 

  //如果允许要调用resignFirstResponder方法,这回导致结束编辑,而键盘会被收起 

  [textField resignFirstResponder];//查一下resign这个单词的意思就明白这个方法了 

  return YES;  

}


通知

UITextField派生自UIControl,所以UIControl类中的通知系统在文本字段中也可以使用。除了UIControl类的标准事件,你还可以使用下列UITextField类特有的事件

UITextFieldTextDidBeginEditingNotification

UITextFieldTextDidChangeNotification

UITextFieldTextDidEndEditingNotification

当文本字段退出编辑模式时触发。通知的object属性存储了最终文本。

因为文本字段要使用键盘输入文字,所以下面这些事件发生时,也会发送动作通知

UIKeyboardWillShowNotification 

键盘显示之前发送


UIKeyboardDidShowNotification

键盘显示之后发送


UIKeyboardWillHideNotification

键盘隐藏之前发送


UIKeyboardDidHideNotification

键盘隐藏之后发送




打开键盘卷动文本字段,下面附一个通用的解决键盘遮挡的方法

//用于处理键盘遮挡的问题

- (void)moveView:(UITextField *)textField leaveView:(BOOL)leave 

  UIView *accessoryView = textField.inputAccessoryView; 

  UIView *inputview   = textField.inputView; 


  int textFieldY = 0; 

  int accessoryY = 0; 


  if (accessoryView && inputview)  

  { 

    CGRect accessoryRect = accessoryView.frame; 

    CGRect inputViewRect = inputview.frame; 

    accessoryY = 480 - (accessoryRect.size.height + inputViewRect.size.height); 

  } 

  else if (accessoryView) 

  { 

    CGRect accessoryRect = accessoryView.frame; 

    accessoryY = 480 - (accessoryRect.size.height + 216); 

  } 

  else if (inputview) 

  { 

    CGRect inputViewRect = inputview.frame; 

    accessoryY = 480 -inputViewRect.size.height; 

  } 

  else 

  { 

    accessoryY = 264; //480 - 216; 

  } 


  CGRect textFieldRect = textField.frame; 

  textFieldY = textFieldRect.origin.y + textFieldRect.size.height + 20; 


  int offsetY = textFieldY - accessoryY; 

  if (!leave && offsetY > 0)  

  { 

    int y_offset = -5; 

    y_offset += -offsetY; 


    CGRect viewFrame = self.view.frame; 

    viewFrame.origin.y += y_offset; 


    [UIView beginAnimations:nil context:NULL]; 

    [UIView setAnimationBeginsFromCurrentState:YES]; 

    [UIView setAnimationDuration:0.3]; 

    [self.view setFrame:viewFrame]; 

    [UIView commitAnimations]; 

  } 

  else 

  { 

    CGRect viewFrame = CGRectMake(0, 20, 320, 460); 


    [UIView beginAnimations:nil context:NULL]; 

    [UIView setAnimationBeginsFromCurrentState:YES]; 

    [UIView setAnimationDuration:0.3]; 

    [self.view setFrame:viewFrame]; 

    [UIView commitAnimations]; 

  } 

}


使用如下:

- (void)textFieldDidBeginEditing:(UITextField *)textField 

  [self moveView:textField leaveView:NO]; 


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

  [self moveView:textField leaveView:YES]; 

}


下面再补充一些重写绘制相关内容


重写绘制行为

除了UITextField对象的风格选项,你还可以定制化UITextField对象,为他添加许多不同的重写方法,来改变文本字段的显示行为。这些方法都会返回一个CGRect结构,制定了文本字段每个部件的边界范围。如果你创见了一个自定义的UITextField类,你可以重写这些方法,这样就可以改变一个或多个边界。一定不要直接调用fan广发;它们都是被iPhone运行库调用的回调函数下面举个例子:

- (CGRect)clearButtonForBounds:(CGRect)bounds{  

  return CGRectMake(bounds.origin.x +bounds.size.width-50,  

           bounds.origin.y+bounds.size.height-20, 16, 16);  

}  


下列方法在创建一个UITextField的子类时可以重写:

borderRectForBounds

指定矩形边界


textRectForBounds

指定显示文本的边界


placeholderRectForBounds

指定站位文本的边界


editingRectForBounds

指定编辑中文本的边界


clearButtonRectForBounds

指定显示清除按钮的边界


leftViewRectForBounds

指定显示左附着视图的边界


rightViewRectForBounds

指定显示右附着视图的边界





下面附带一个UILabelUITextField的重绘实现padding效果的代码



首先来看 UILabel 的子类InsetsLabel 的实现代码:

//1.header file

#import


@interface InsetsLabel : UILabel


@property(nonatomic) UIEdgeInsets insets;


-(id) initWithFrame:(CGRect)frame andInsets: (UIEdgeInsets) insets;

-(id) initWithInsets: (UIEdgeInsets) insets;

@end


//2. implementation file

#import "InsetsLabel.h"


@implementation InsetsLabel

@synthesize insets=_insets;


-(id) initWithFrame:(CGRect)frame andInsets:(UIEdgeInsets)insets {

  self = [super initWithFrame:frame];

  if(self){

    self.insets = insets;

  }

  return self;

}


-(id) initWithInsets:(UIEdgeInsets)insets {

  self = [super init];

  if(self){

    self.insets = insets;

  }

  return self;

}


-(void) drawTextInRect:(CGRect)rect {

  return [super drawTextInRect:UIEdgeInsetsInsetRect(rect, self.insets)];

}


关键就是覆盖了-(void) drawTextInRect: (CGRect) rect; 方法,在画  Label 的文本时分别设置文本与  Label 四个边的间隙,即画在 Label 内的一个小矩形内,这个例子提供了便利的构造函数,提供自己的UIEdgeInsets 属性。另外,函数 UIEdgeInsetsInsetRect(CGRect, UIEdgeInsets) 应该是好理解的。

再看如何设置 UITextField 中文本到四边的间距,这里也可以定义自己的 InsetsTextField

//

// Created by Unmi on 11/2/11.

// Copyright (c) 2011 http://unmi.cc. All rights reserved.

//

#import


@interface InsetsTextField : UITextField

@end


@implementation InsetsTextField

//控制placeHolder 的位置,左右缩20

- (CGRect)textRectForBounds:(CGRect)bounds {

  return CGRectInset( bounds , 20 , 0 );

  // CGRect inset = CGRectMake(bounds.origin.x + 10, bounds.origin.y, bounds.size.width - 10, bounds.size.height); //更好理解些

  // return inset;

}


//控制文本的位置,左右缩 20

- (CGRect)editingRectForBounds:(CGRect)bounds {

  return CGRectInset( bounds , 20 , 0 );

  //CGRect inset = CGRectMake(bounds.origin.x + 10, bounds.origin.y, bounds.size.width - 10, bounds.size.height);

  // return inset;

}

@end


//-----------------------------------------------------------------

//下面是使用InsetsTextField 的代码,可放在viewDidLoad 等代理方法中

InsetsTextField *insetTextField = [[InsetsTextField alloc]

                  initWithFrame:CGRectMake(10, 10, 180, 25)];

//须手动设置它的borderStyle, 不然看不到边框的

insetsTextField.borderStyle = UITextBorderStyleRoundedRect;

[self.view addSubview:insetsTextField];

[insetsTextField release];


@end


效果如下:

上面更像是借鉴的 InsetsLabel 的实现,其实对于UITextField 还有更好的实现办法,而且更简单,因为 UITextFiled 原来就支持的做法。比如它可以让你做出在文本框最前方固定一个$符号,表示这个文本框是输入钱的,第一个$是不能被删除的。确实,你可以在 TextField 上贴个 Label,然后文本框的光标后移,稍显麻烦了。

UITextField 可以直接设置leftView rightView, 然后文本输入区域就在leftView rightView 之间了,看例子:

UILabel *paddingView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 10, 25)];

paddingView.text = @"$";

paddingView.textColor = [UIColor darkGrayColor];

paddingView.backgroundColor = [UIColor clearColor];

textfield.leftView = paddingView;

textfield.leftViewMode = UITextFieldViewModeAlways;rightView 也是一样的设置方式,其中的 Mode 有四种,看到名字应该不难理解:

   UITextFieldViewModeNever,
   UITextFieldViewModeWhileEditing,
   UITextFieldViewModeUnlessEditing,
   UITextFieldViewModeAlways

它的效果呢就更酷了:

文本框的起始光标是从上图数字位置开始的。

实际应用中,对于 UITextField 如果有类似的需求,我会毫不犹豫的使用leftView/rightView 属性来设置。

1.—设置 UILabel 和 UITextField 的 Padding 或 Insets

2.—NIB中使用方法

来自: http://my.oschina.net/bieshixuan/blog/604418