TextField的语法样例大全

  1. #import "ViewController.h"
  2. www.gywcjb120.com
  3. #define kScreenSize [UIScreen mainScreen].bounds.size
  4. #define kDebugPrint NSLog(@"%s %d",__func__,__LINE__)
  5. @interface ViewController ()<UITextFieldDelegate>
  6. @end
  7. www.gywcjb120.com
  8. @implementation ViewController
  9. - (void)viewDidLoad {
  10. [super viewDidLoad];
  11. [self creatTextField];
  12. }
  13. //- (BOOL)textFieldShouldReturn:(UITextField *)textField{
  14. //
  15. // return YES;
  16. //
  17. //}
  18. #pragma mark - 文本输入框
  19. - (void)creatTextField {
  20. NSInteger space = 10;
  21. UITextField *textField1 = [[UITextField alloc] initWithFrame:CGRectMake(space, 30, kScreenSize.width-2*space, 30)];
  22. //设置背景
  23. //textField1.backgroundColor = [UIColor redColor];
  24. //设置边框类型
  25. /*
  26. UITextBorderStyleNone,
  27. UITextBorderStyleLine,
  28. UITextBorderStyleBezel,
  29. UITextBorderStyleRoundedRect
  30. */
  31. www.gywcjb120.com
  32. textField1.borderStyle = UITextBorderStyleLine;
  33. //设置提示语
  34. textField1.placeholder = @"请输入内容";
  35. //设置对齐方式 水平
  36. textField1.textAlignment = NSTextAlignmentCenter;
  37. //竖直对齐
  38. textField1.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;//顶部对齐
  39. //设置字体大小
  40. textField1.font = [UIFont systemFontOfSize:25];
  41. //字体大小 宽度自适应
  42. textField1.adjustsFontSizeToFitWidth = YES;
  43. //设置自适应滚动效果的字体最小值
  44. textField1.minimumFontSize = 20;
  45. //设置字体颜色
  46. www.gywcjb120.com
  47. textField1.textColor = [UIColor redColor];
  48. //再次进入编辑模式 是否清除之前内容
  49. textField1.clearsOnBeginEditing = YES;
  50. //设置 右侧清除按钮 小叉子
  51. /*
  52. www.gywcjb120.com
  53. UITextFieldViewModeNever,
  54. UITextFieldViewModeWhileEditing,编辑的时候显示
  55. UITextFieldViewModeUnlessEditing,输入内容之后退出编辑模式的时候(不编辑的时候)
  56. UITextFieldViewModeAlways
  57. */
  58. textField1.clearButtonMode = UITextFieldViewModeUnlessEditing;
  59. //设置键盘的风格
  60. textField1.keyboardAppearance = UIKeyboardAppearanceDark;
  61. //设置键盘的类型 电话键盘 数字键盘 邮箱键盘
  62. //textField1.keyboardType = UIKeyboardTypeNumberPad;
  63. //设置return键
  64. /*
  65. www.gywcjb120.com
  66. UIReturnKeyDefault,
  67. UIReturnKeyGo,
  68. UIReturnKeyGoogle,
  69. UIReturnKeyJoin,
  70. UIReturnKeyNext,
  71. UIReturnKeyRoute,
  72. UIReturnKeySearch,
  73. UIReturnKeySend,
  74. UIReturnKeyYahoo,
  75. UIReturnKeyDone,
  76. UIReturnKeyEmergencyCall,
  77. */www.gywcjb120.com
  78. textField1.returnKeyType = UIReturnKeySend;
  79. //设置输入内容的首字母是否大写
  80. /*www.gywcjb120.com
  81. UITextAutocapitalizationTypeNone, 都不大写
  82. UITextAutocapitalizationTypeWords, 单词首字母大写
  83. UITextAutocapitalizationTypeSentences,句子首字母大写
  84. UITextAutocapitalizationTypeAllCharacters,都大写
  85. */
  86. textField1.autocapitalizationType = UITextAutocapitalizationTypeWords;
  87. /*www.gywcjb120.com
  88. UITextAutocorrectionTypeDefault 自动纠错
  89. UITextAutocorrectionTypeNo, 不纠错
  90. UITextAutocorrectionTypeYes, 自动纠错
  91. */www.gywcjb120.com
  92. //设置自动纠错
  93. textField1.autocorrectionType = UITextAutocorrectionTypeYes;
  94. www.gywcjb120.com

  95. [self.view addSubview:textField1];
  96. [textField1 release];
  97. //根据textField1.frame 获取 frame y+height的值
  98. CGFloat y = CGRectGetMaxY(textField1.frame);
  99. UITextField *textField2 = [[UITextField alloc] initWithFrame:CGRectMake(space, y+space, kScreenSize.width-2*space, 30)];
  100. textField2.borderStyle = UITextBorderStyleLine;
  101. //密文显示
  102. textField2.secureTextEntry = YES;
  103. www.gywcjb120.com
  104. //获取内容
  105. //textField2.text
  106. www.gywcjb120.com
  107. [self.view addSubview:textField2];
  108. [textField2 release];
  109. }
  110. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
  111. NSLog(@"range:%@",NSStringFromRange(range));
  112. NSLog(@"string:%@",string);
  113. //限制密码textField 输入只能输入6位
  114. if (textField.tag == 102) {//密码textField
  115. //将要输入的字符长度 + 已经输入的字符长度 <= 6
  116. return textField.text.length+string.length <= 6;
  117. }
  118. return YES;
  119. }
  120. www.gywcjb120.com
  121. - (void)didReceiveMemoryWarning {
  122. [super didReceiveMemoryWarning];
  123. // Dispose of any resources that can be recreated.
  124. }
  125. www.gywcjb120.com
  126. @end
复制代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值