控件--UITextField

来源:互联网 发布:眼镜店软件 编辑:程序博客网 时间:2024/06/08 07:21
[plain] view plaincopy
  1. nameField = [[UITextField alloc] init];  
  2. nameField.frame = CGRectMake(35, 130, 250, 35);  
  3. nameField.delegate = self;  
  4. //边界风格  
  5. nameField.borderStyle = UITextBorderStyleRoundedRect;  
  6. //文字在竖直方向的位置  
  7. nameField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;  
  8. //没有输入时显示的文字,输入之后小时,作提示用  
  9. nameField.placeholder = @"please input your email:";  
  10. //键盘返回键类型  
  11. nameField.returnKeyType = UIReturnKeyDone;  
  12. //当编辑的时候有一个x,提示可以一次删除UITextField中全部内容,需要代理配合实现  
  13. nameField.clearButtonMode = UITextFieldViewModeWhileEditing;  
  14. //键盘类型   
  15. nameField.keyboardType = UIKeyboardTypeEmailAddress;  
  16. //标记  
  17. nameField.tag = 1001;  
  18. //文本框中文本的颜色  
  19. nameField.textColor = [UIColor blueColor];  
  20. //设置文本框可以显示的最小字体,minimumFontSize,adjustsFontSizeToFitWidth必须配合使用才有效果  
  21. nameField.minimumFontSize = 8;  
  22. nameField.adjustsFontSizeToFitWidth = YES;  
  23. //输入文字的字体  
  24. nameField.font = [UIFont systemFontOfSize:20];  
  25. //只有至少在文本框输入一个字符后键盘的返回键才有效  
  26. nameField.enablesReturnKeyAutomatically = YES;  
  27. //密码风格  
  28. nameField.secureTextEntry = YES;  
  29. //设置大写  
  30. //None : 不设置大写  
  31. //Words : 每个单词首字母大写,这里的单词指的是以空格分开的字符串  
  32. //Sentances : 每个句子的第一个字母大写,这里的句子是以句号加空格分开的字符串  
  33. //All Characters : 所以字母大写  
  34. nameField.autocapitalizationType = UITextAutocapitalizationTypeSentences;  
  35. [self.view addSubview:nameField];  


常用代理方法:

[plain] view plaincopy
  1. 1.开始编辑:  
  2. - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;  
  3.   
  4. 2.点击return按钮所做的动作:  
  5. - (BOOL)textFieldShouldReturn:(UITextField *)textField;  
  6.   
  7. 3.是否一次完全清除:  
  8. - (BOOL)textFieldShouldClear:(UITextField *)textField;  
  9.   
  10. 4.编辑完成:  
  11. - (void)textFieldDidEndEditing:(UITextField *)textField;  

文本为空的时候,send(发送)按钮不可用:

[plain] view plaincopy
  1. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string  
  2. {  
  3.     NSMutableString *newValue = [[textField.text mutableCopy] autorelease];  
  4.     [newValue replaceCharactersInRange:range withString:string];//string是当前输入的字符,newValue是当前输入框中的字符  
  5.     if ([newValue length]== 0)  
  6.         {  
  7.         sendBtn.enabled =  NO;  
  8.     }  
  9.     else  
  10.         {  
  11.         sendBtn.enabled = YES;  
  12.     }  
  13.     return YES;  


原创粉丝点击