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];
隐藏keyboard
[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 看如下例子 其中- #define ALPHA @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz "
- #define NUMBERS @"0123456789"
- #define ALPHANUM @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 "
- #define NUMBERSPERIOD @"0123456789."
- - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
- {NSCharacterSet *cs;
- cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERSPERIOD ] invertedSet]; //invertedSet 方法是去反字符,把所有的除了数字的字符都找出来
- NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""]; //componentsSeparatedByCharactersInSet 方法是把输入框输入的字符string 根据cs中字符一个一个去除cs字符并分割成单字符并转化为 NSArray, 然后componentsJoinedByString 是把NSArray 的字符通过 ""无间隔连接成一个NSString字符 赋给filtered.就是只剩数字了.
- BOOL basicTest = [string isEqualToString:filtered];
- if(!basicTest)
- {
- UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示"
- message:@"请输入数字"
- delegate:nil
- cancelButtonTitle:@"确定"
- otherButtonTitles:nil];
- [alert show];
- [alert release];
- return NO;
- }
- // Add any predicate testing here
- return basicTest;
- }
- // add target/action for particular event. you can call this multiple times and you can specify multiple target/actions for a particular event.
- // passing in nil as the target goes up the responder chain. The action may optionally include the sender and the event in that order
- // the action cannot be NULL.
- - (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
- //第一步,对组件增加监听器 可以在viewDidLoad 方法中加入 textField 为你自定义输入框的名称
- [textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
- ...
- //第二步,实现回调函数
- - (void) textFieldDidChange:(id) sender {
- UITextField *_field = (UITextField *)sender;
- NSLog(@"%@",[_field text]);
- }