UITextField是我们经常用的之一但是常用的属性都很熟悉,有些不常用的我也总结下,例如下面的:

UIImageView * myView = [[ UIImageView  alloc]initWithImage:[UIImage imageNamed:@"face.png"]]; 
 UIImageView * myView2 = [[ UIImageView  alloc]initWithImage:[UIImage imageNamed:@"face.png"]]; 
 UITextField *myTextField=[[UITextField alloc]initWithFrame:CGRectMake(40, 40, 240, 60)]; //初始化一个UITextField的frame 
 myTextField.textColor=[UIColor redColor];  //UITextField 的文字颜色 
 myTextField.delegate=self;//UITextField 代理方法设置 
 myTextField.placeholder=@"输入密码";//UITextField 的初始隐藏文字,当然这个文字的字体大小颜色都可以改,重写uitextfield,下次介绍 
 myTextField.textAlignment=UITextAlignmentCenter;//UITextField 的文字对齐格式 
 myTextField.font=[UIFont fontWithName:@"Times New Roman" size:30];//UITextField 的文字大小和字体 
 myTextField.adjustsFontSizeToFitWidth=YES;//UITextField 的文字自适应 
 myTextField.clearsOnBeginEditing=NO;//UITextField 的是否出现一件清除按钮 
 myTextField.borderStyle=UITextBorderStyleNone;//UITextField 的边框 
 myTextField.background=[UIImage imageNamed:@"my.png"];//UITextField 的背景,注意只有UITextBorderStyleNone的时候改属性有效 
 myTextField.clearButtonMode=UITextFieldViewModeNever;//UITextField 的一件清除按钮是否出现 
 myTextField.leftView=myView;//UITextField 的左边view 
 myTextField.leftViewMode=UITextFieldViewModeAlways;//UITextField 的左边view 出现模式 
 myTextField.rightView=myView2;//UITextField 的有边view 
 myTextField.rightViewMode=UITextFieldViewModeAlways;//UITextField 的右边view 出现模式 
 myTextField.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;//UITextField 的字的摆设方式 
 [myView release]; 
 [myView2 release]; 
 [self.view addSubview:myTextField];

当然myTextField的键盘的出现也隐藏也可以设置:

显示keyboard:



[myTextField becomeFirstResponder];
[myTextField becomeFirstResponder];



隐藏keyboard



[myTextField resignFirstResponder];
[myTextField resignFirstResponder];



myTextField.contentVerticalAlignment的值的种类: 
 typedef enum {
     UIControlContentVerticalAlignmentCenter  = 0,
     UIControlContentVerticalAlignmentTop     = 1,
     UIControlContentVerticalAlignmentBottom  = 2,
     UIControlContentVerticalAlignmentFill    = 3,
 } UIControlContentVerticalAlignment;  
 
 
   转载别人的。。。 
 // 限定字符串 
 在 Xcode interface builder 可以通过可视化 设置输入框 的 属性 在Attribute inspector 里面把输入设成Numberpad (在Xcode4.2中 为 keyboard属性 选择Number Pad)



但是如果用户在输入框直接粘贴其他地方复制的非数字的话,键盘限制就无用了

可以使用 UITextField 的代理Delegate 方法
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 
看如下例子
其中 



复制代码

1.  #define ALPHA @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz " 
2.  #define NUMBERS @"0123456789" 
3.  #define ALPHANUM @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 " 
4.  #define NUMBERSPERIOD @"0123456789." 
5. 
6. 
7.  - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 
8. 
9.  {NSCharacterSet *cs; 
10.  cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERSPERIOD ] invertedSet]; //invertedSet 方法是去反字符,把所有的除了数字的字符都找出来 
11. 
12.  NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];  //componentsSeparatedByCharactersInSet 方法是把输入框输入的字符string 根据cs中字符一个一个去除cs字符并分割成单字符并转化为 NSArray, 然后componentsJoinedByString 是把NSArray 的字符通过 ""无间隔连接成一个NSString字符 赋给filtered.就是只剩数字了. 
13. 
14. 
15.  BOOL basicTest = [string isEqualToString:filtered]; 
16.  if(!basicTest) 
17. 
18.          { 
19.              UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示" 
20.                                                              message:@"请输入数字" 
21.                                                             delegate:nil 
22.                                                    cancelButtonTitle:@"确定" 
23.                                                    otherButtonTitles:nil]; 
24.               
25.              [alert show]; 
26.              [alert release]; 
27.              return NO; 
28.          }     
29. 
30.  // Add any predicate testing here 
31.  return basicTest; 
32.  }

NSString 官方文档

http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nsstring_Class/Reference/NSString.html

NSArray 官方文档

http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/NSArray.html#//apple_ref/doc/c_ref/NSArray




参考



http://guxiaojje.blog.163.com/blog/static/14094229120104344438786/

http://portal.devdiv.com/thread-70160-1-1.html




在UISearchBar中,当输入信息改变时,它就会调用textDidChange函数,但是UITextField没有这个功能,唯一与这个类似的shouldChangeCharactersInRange函数,也是在文件还没有改变前就调用了,而不是在改变后调用,要想实现这个功能,我们可以增加事件监听的方式,这个与java的listener类似.先来看看objective-c提供的接口:


复制代码


1.  // add target/action for particular event. you can call this multiple times and you can specify multiple target/actions for a particular event. 
2.  // passing in nil as the target goes up the responder chain. The action may optionally include the sender and the event in that order 
3.  // the action cannot be NULL. 
4.  - (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;


怎么去使用这个接口呢?主要分为两步,第一步就是在UItextField组件中增加对文件编辑改变时事件的监听,然后再实现监听器监听到事件时,所调用的方法.


复制代码


1.  //第一步,对组件增加监听器 可以在viewDidLoad 方法中加入 textField 为你自定义输入框的名称 
2. 
3.  [textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged]; 
4.  ... 
5.  //第二步,实现回调函数 
6.  - (void) textFieldDidChange:(id) sender { 
7.      UITextField *_field = (UITextField *)sender; 
8.      NSLog(@"%@",[_field text]); 
9.  }