#import "ViewController.h" #define kScreenSize [UIScreen mainScreen].bounds.size #define kDebugPrint NSLog(@"%s %d",__func__,__LINE__) @interface ViewController ()<UITextFieldDelegate> @end @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 */ textField1.borderStyle = UITextBorderStyleLine; //設置提示語 textField1.placeholder = @"請輸入內容"; //設置對齊方式 水平 textField1.textAlignment = NSTextAlignmentCenter; //豎直對齊 textField1.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;//頂部對齊 //設置字體大小 textField1.font = [UIFont systemFontOfSize:25]; //字體大小 寬度自適應 textField1.adjustsFontSizeToFitWidth = YES; //設置自適應滾動效果的字體最小值 textField1.minimumFontSize = 20; //設置字體顏色 textField1.textColor = [UIColor redColor]; //再次進入編輯模式 是否清除以前內容 textField1.clearsOnBeginEditing = YES; //設置 右側清除按鈕 小叉子 /* UITextFieldViewModeNever, UITextFieldViewModeWhileEditing,編輯的時候顯示 UITextFieldViewModeUnlessEditing,輸入內容以後退出編輯模式的時候(不編輯的時候) UITextFieldViewModeAlways */ textField1.clearButtonMode = UITextFieldViewModeUnlessEditing; //設置鍵盤的風格 textField1.keyboardAppearance = UIKeyboardAppearanceDark; //設置鍵盤的類型 電話鍵盤 數字鍵盤 郵箱鍵盤 //textField1.keyboardType = UIKeyboardTypeNumberPad; //設置return鍵 /* UIReturnKeyDefault, UIReturnKeyGo, UIReturnKeyGoogle, UIReturnKeyJoin, UIReturnKeyNext, UIReturnKeyRoute, UIReturnKeySearch, UIReturnKeySend, UIReturnKeyYahoo, UIReturnKeyDone, UIReturnKeyEmergencyCall, */ textField1.returnKeyType = UIReturnKeySend; //設置輸入內容的首字母是否大寫 /* UITextAutocapitalizationTypeNone, 都不大寫 UITextAutocapitalizationTypeWords, 單詞首字母大寫 UITextAutocapitalizationTypeSentences,句子首字母大寫 UITextAutocapitalizationTypeAllCharacters,都大寫 */ textField1.autocapitalizationType = UITextAutocapitalizationTypeWords; /* UITextAutocorrectionTypeDefault 自動糾錯 UITextAutocorrectionTypeNo, 不糾錯 UITextAutocorrectionTypeYes, 自動糾錯 */ //設置自動糾錯 textField1.autocorrectionType = UITextAutocorrectionTypeYes; [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; //獲取內容 //textField2.text [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; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end