iOS开发系列之三 - UITextField 用法小结

来源:互联网 发布:ubuntu安装第三方deb 编辑:程序博客网 时间:2024/06/06 07:31
  1. // 初始化输入框并设置位置和大小  
  2. UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(1010030030)];  
  3. // 设置输入框提示  
  4. textField.placeholder = @"TextField Tip";  
  5. // 输入框中预先输入的文字  
  6. textField.text = @"预先输入的文字";  
  7. // 设置输入框文本的字体  
  8. textField.font = [UIFont fontWithName:@"Arial" size:20.0f];  
  9. // 设置输入框字体颜色  
  10. textField.textColor = [UIColor redColor];  
  11. // 设置输入框的背景颜色  
  12. textField.backgroundColor = [UIColor grayColor];  
  13. // 设置输入框边框样式  
  14. textField.borderStyle = UITextBorderStyleRoundedRect;  
  15.   
  16. // 边框样式有以下几种:  
  17. //    enum {  
  18. //        UITextBorderStyleNone,        无边框,默认  
  19. //        UITextBorderStyleLine,        有线型边框  
  20. //        UITextBorderStyleBezel,       有线型边框和阴影  
  21. //        UITextBorderStyleRoundedRect  有圆角边框  
  22. //    } UITextBorderStyle;  
  23.   
  24. // 设置输入框是否用于密码  
  25. textField.secureTextEntry = NO;  
  26. // 设置是否有清除按钮,在什么时候显示,用于一次性删除输入框中的所有内容  
  27. textField.clearButtonMode = UITextFieldViewModeWhileEditing;  
  28.   
  29. // 清除按钮样式有以下几种:  
  30. //    enum {  
  31. //        UITextFieldViewModeNever,          从不出现  
  32. //        UITextFieldViewModeWhileEditing,   编辑时出现  
  33. //        UITextFieldViewModeUnlessEditing,  除了编辑外都出现  
  34. //        UITextFieldViewModeAlways          一直出现  
  35. //    } UITextFieldViewMode;  
  36.   
  37. // 设置自动纠错方式  
  38. textField.autocorrectionType = UITextAutocorrectionTypeNo;  
  39.   
  40. // 自动纠错方式有以下几种:  
  41. //    enum {  
  42. //        UITextAutocorrectionTypeDefault,  默认  
  43. //        UITextAutocorrectionTypeNo,       不自动纠错  
  44. //        UITextAutocorrectionTypeYes,      自动纠错  
  45. //    } UITextAutocorrectionType;  
  46.   
  47. // 设置自动大写方式  
  48. textField.autocapitalizationType = UITextAutocapitalizationTypeNone;  
  49.   
  50. // 自动大写方式有以下几种:  
  51. //    enum {  
  52. //        UITextAutocapitalizationTypeNone,           不自动大写  
  53. //        UITextAutocapitalizationTypeWords,          单词首字母大写  
  54. //        UITextAutocapitalizationTypeSentences,      句子的首字母大写  
  55. //        UITextAutocapitalizationTypeAllCharacters,  所有字母都大写  
  56. //    } UITextAutocapitalizationType;  
  57.   
  58. // 设置再次编辑是否清空  
  59. textField.clearsOnBeginEditing = YES;  
  60. // 设置文本对齐方式  
  61. textField.textAlignment = NSTextAlignmentLeft;  
  62.   
  63. // iOS7中文本对齐方式有以下几种:  
  64. //    enum {  
  65. //        NSTextAlignmentLeft      = 0,  左对齐,默认  
  66. //        NSTextAlignmentCenter    = 1,  居中对齐  
  67. //        NSTextAlignmentRight     = 2,  右对齐  
  68. //        NSTextAlignmentJustified = 3,  在一个段落的最后一行自然对齐  
  69. //        NSTextAlignmentNatural   = 4,  默认对齐方式  
  70. //    } NSTextAlignment;  
  71.   
  72. // 设置字体大小是否自动适应输入框宽度,默认是保持原来大小,长文本滚动  
  73. textField.adjustsFontSizeToFitWidth = YES;  
  74. // 设置自动缩小显示的最小字体大小  
  75. textField.minimumFontSize = 20;  
  76. // 设置键盘的样式  
  77. textField.keyboardType = UIKeyboardTypeNumberPad;  
  78.   
  79. // 键盘样式有以下几种:  
  80. //    enum {  
  81. //        UIKeyboardTypeDefault,                默认键盘,支持所有字符  
  82. //        UIKeyboardTypeASCIICapable,           支持ASCII的默认键盘  
  83. //        UIKeyboardTypeNumbersAndPunctuation,  标准电话键盘,支持+*#字符  
  84. //        UIKeyboardTypeURL,                    只支持URL字符的URL键盘,支持.com按钮  
  85. //        UIKeyboardTypeNumberPad,              数字键盘  
  86. //        UIKeyboardTypePhonePad,               电话键盘  
  87. //        UIKeyboardTypeNamePhonePad,           支持输入人名的电话键盘  
  88. //        UIKeyboardTypeEmailAddress,           电子邮件键盘  
  89. //        UIKeyboardTypeDecimalPad,             有数字和小数点的数字键盘  
  90. //        UIKeyboardTypeTwitter,                优化的键盘,方便输入@、#字符  
  91. //        UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable,  
  92. //    } UIKeyboardType;  
  93.   
  94. // 设置return键样式  
  95. textField.returnKeyType = UIReturnKeyDone;  
  96.   
  97. // return键有以下几种样式:  
  98. //    enum {  
  99. //        UIReturnKeyDefault,        默认,灰色按钮,标有Return  
  100. //        UIReturnKeyGo,             标有Go的蓝色按钮  
  101. //        UIReturnKeyGoogle,         标有Google的蓝色按钮,用于搜索  
  102. //        UIReturnKeyJoin,           标有Join的蓝色按钮  
  103. //        UIReturnKeyNext,           标有Next的蓝色按钮  
  104. //        UIReturnKeyRoute,          标有Route的蓝色按钮  
  105. //        UIReturnKeySearch,         标有Search的蓝色按钮  
  106. //        UIReturnKeySend,           标有Send的蓝色按钮  
  107. //        UIReturnKeyYahoo,          标有Yahoo的蓝色按钮  
  108. //        UIReturnKeyYahoo,          标有Yahoo的蓝色按钮  
  109. //        UIReturnKeyEmergencyCall,  紧急呼叫按钮  
  110. //    } UIReturnKeyType;  
  111.    
  112. // 设置键盘外观  
  113. textField.keyboardAppearance = UIKeyboardAppearanceDefault;  
  114.   
  115. // 键盘外观有一下两种:  
  116. //    enum {  
  117. //        UIKeyboardAppearanceDefault, 默认外观,浅灰色  
  118. //        UIKeyboardAppearanceAlert,   深灰,石墨色  
  119. //    } UIReturnKeyType;  
  120.   
  121. // 设置代理,用于实现协议  
  122. textField.delegate = self;  
  123.    
  124. // 最右侧加图片是以下代码,左侧类似  
  125. UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"right.png"]];  
  126. textField.rightView = image;  
  127. textField.rightViewMode = UITextFieldViewModeAlways;  
  128.   
  129. // 把输入框加到视图中  
  130. [self.view addSubview:textField];  
  131.   
  132. // 按return键收起键盘  
  133. - (BOOL)textFieldShouldReturn:(UITextField *)textField  
  134. {  
  135.     [text resignFirstResponder];  
  136.     return YES;  
0 0