- #import "ViewController.h"
- www.gywcjb120.com
- #define kScreenSize [UIScreen mainScreen].bounds.size
- #define kDebugPrint NSLog(@"%s %d",__func__,__LINE__)
- @interface ViewController ()<UITextFieldDelegate>
- @end
- www.gywcjb120.com
- @implementation ViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- [self creatTextField];
- }
- //- (BOOL)textFieldShouldReturn:(UITextField *)textField{
- //
- // return YES;
- //
- //}
- #pragma mark - 文本输入框
- - (void)creatTextField {
- NSInteger space = 10;
- UITextField *textField1 = [[UITextField alloc] initWithFrame:CGRectMake(space, 30, kScreenSize.width-2*space, 30)];
- //设置背景
- //textField1.backgroundColor = [UIColor redColor];
- //设置边框类型
- /*
- UITextBorderStyleNone,
- UITextBorderStyleLine,
- UITextBorderStyleBezel,
- UITextBorderStyleRoundedRect
- */
- www.gywcjb120.com
- textField1.borderStyle = UITextBorderStyleLine;
- //设置提示语
- textField1.placeholder = @"请输入内容";
- //设置对齐方式 水平
- textField1.textAlignment = NSTextAlignmentCenter;
- //竖直对齐
- textField1.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;//顶部对齐
- //设置字体大小
- textField1.font = [UIFont systemFontOfSize:25];
- //字体大小 宽度自适应
- textField1.adjustsFontSizeToFitWidth = YES;
- //设置自适应滚动效果的字体最小值
- textField1.minimumFontSize = 20;
- //设置字体颜色
- www.gywcjb120.com
- textField1.textColor = [UIColor redColor];
- //再次进入编辑模式 是否清除之前内容
- textField1.clearsOnBeginEditing = YES;
- //设置 右侧清除按钮 小叉子
- /*
- www.gywcjb120.com
- UITextFieldViewModeNever,
- UITextFieldViewModeWhileEditing,编辑的时候显示
- UITextFieldViewModeUnlessEditing,输入内容之后退出编辑模式的时候(不编辑的时候)
- UITextFieldViewModeAlways
- */
- textField1.clearButtonMode = UITextFieldViewModeUnlessEditing;
- //设置键盘的风格
- textField1.keyboardAppearance = UIKeyboardAppearanceDark;
- //设置键盘的类型 电话键盘 数字键盘 邮箱键盘
- //textField1.keyboardType = UIKeyboardTypeNumberPad;
- //设置return键
- /*
- www.gywcjb120.com
- UIReturnKeyDefault,
- UIReturnKeyGo,
- UIReturnKeyGoogle,
- UIReturnKeyJoin,
- UIReturnKeyNext,
- UIReturnKeyRoute,
- UIReturnKeySearch,
- UIReturnKeySend,
- UIReturnKeyYahoo,
- UIReturnKeyDone,
- UIReturnKeyEmergencyCall,
- */www.gywcjb120.com
- textField1.returnKeyType = UIReturnKeySend;
- //设置输入内容的首字母是否大写
- /*www.gywcjb120.com
- UITextAutocapitalizationTypeNone, 都不大写
- UITextAutocapitalizationTypeWords, 单词首字母大写
- UITextAutocapitalizationTypeSentences,句子首字母大写
- UITextAutocapitalizationTypeAllCharacters,都大写
- */
- textField1.autocapitalizationType = UITextAutocapitalizationTypeWords;
- /*www.gywcjb120.com
- UITextAutocorrectionTypeDefault 自动纠错
- UITextAutocorrectionTypeNo, 不纠错
- UITextAutocorrectionTypeYes, 自动纠错
- */www.gywcjb120.com
- //设置自动纠错
- textField1.autocorrectionType = UITextAutocorrectionTypeYes;
- www.gywcjb120.com
-
- [self.view addSubview:textField1];
- [textField1 release];
- //根据textField1.frame 获取 frame y+height的值
- CGFloat y = CGRectGetMaxY(textField1.frame);
- UITextField *textField2 = [[UITextField alloc] initWithFrame:CGRectMake(space, y+space, kScreenSize.width-2*space, 30)];
- textField2.borderStyle = UITextBorderStyleLine;
- //密文显示
- textField2.secureTextEntry = YES;
- www.gywcjb120.com
- //获取内容
- //textField2.text
- www.gywcjb120.com
- [self.view addSubview:textField2];
- [textField2 release];
- }
- - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
- NSLog(@"range:%@",NSStringFromRange(range));
- NSLog(@"string:%@",string);
- //限制密码textField 输入只能输入6位
- if (textField.tag == 102) {//密码textField
- //将要输入的字符长度 + 已经输入的字符长度 <= 6
- return textField.text.length+string.length <= 6;
- }
- return YES;
- }
- www.gywcjb120.com
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- www.gywcjb120.com
- @end
TextField的语法样例大全
最新推荐文章于 2021-08-05 22:20:20 发布