UITextField常用属性

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 
看如下例子
其中 

  1. #define ALPHA @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz "
  2. #define NUMBERS @"0123456789"
  3. #define ALPHANUM @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 "
  4. #define NUMBERSPERIOD @"0123456789."
  5. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
  6. {NSCharacterSet *cs;
  7. cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERSPERIOD ] invertedSet]; //invertedSet 方法是去反字符,把所有的除了数字的字符都找出来
  8. NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];  //componentsSeparatedByCharactersInSet 方法是把输入框输入的字符string 根据cs中字符一个一个去除cs字符并分割成单字符并转化为 NSArray, 然后componentsJoinedByString 是把NSArray 的字符通过 ""无间隔连接成一个NSString字符 赋给filtered.就是只剩数字了.
  9. BOOL basicTest = [string isEqualToString:filtered];
  10. if(!basicTest)
  11.         {
  12.             UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示"
  13.                                                             message:@"请输入数字"
  14.                                                            delegate:nil
  15.                                                   cancelButtonTitle:@"确定"
  16.                                                   otherButtonTitles:nil];
  17.             
  18.             [alert show];
  19.             [alert release];
  20.             return NO;
  21.         }    
  22. // Add any predicate testing here
  23. return basicTest;
  24. }
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. [textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
  3. ...
  4. //第二步,实现回调函数
  5. - (void) textFieldDidChange:(id) sender {
  6.     UITextField *_field = (UITextField *)sender;
  7.     NSLog(@"%@",[_field text]);
  8. }

转载于:https://my.oschina.net/u/936286/blog/120651

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值