#pragma mark - 1.創建TextField - (void)createTextField { UITextField *textFiled = [[UITextField alloc] initWithFrame:CGRectMake(20, 40, 250, 45)]; textF ...
#pragma mark - 1.創建TextField
- (void)createTextField {
UITextField *textFiled = [[UITextField alloc] initWithFrame:CGRectMake(20, 40, 250, 45)];
textFiled.tag = 1;
[self.view addSubview:textFiled];
}
#pragma mark - 2.設置TextFieldOutLook
- (void)setUpTextFieldOutLook {
UITextField *textField = [self.view viewWithTag:1];
//設置邊框
/*
UITextBorderStyleNone,
UITextBorderStyleLine, 線性邊框
UITextBorderStyleBezel, 斜射邊框
UITextBorderStyleRoundedRect 圓角邊框,顯示不出背景圖片
*/
textField.borderStyle = UITextBorderStyleNone;//在圓角邊框下,顯示不出背景圖片
//設置背景圖片
textField.background = [UIImage imageNamed:@"dove"];
//禁止用戶操作 enabled使能夠
textField.enabled = NO;//設置為NO點擊就不響應了
//設置不可操作下的圖片。點擊無用
textField.disabledBackground = [UIImage imageNamed:@"dove2"];
textField.enabled = YES;// 改為Yes 後,可以響應,但圖片是enabled情況下的圖片dove.png
}
#pragma mark - 3.設置文本
- (void)setText {
UITextField *textField = [self.view viewWithTag:1];
// 1.基本文本樣式設置
textField.placeholder = @"請輸入賬號";
textField.font = [UIFont boldSystemFontOfSize:16];
textField.textColor = [UIColor redColor];
textField.textAlignment = NSTextAlignmentLeft;
// 2.顯示清空按鈕
/*
UITextFieldViewModeNever,
UITextFieldViewModeWhileEditing,
UITextFieldViewModeUnlessEditing,
UITextFieldViewModeAlways
*/
textField.clearButtonMode = UITextFieldViewModeAlways;
//3.設置文字自適應內容的寬度,與最小字體
textField.adjustsFontSizeToFitWidth = YES;
textField.minimumFontSize = 5;
//4.設置左邊,右邊的圖片
UIImageView *leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
leftImageView.image = [UIImage imageNamed:@"qq"];
textField.leftView = leftImageView;
textField.leftViewMode = UITextFieldViewModeAlways;
UIImageView *rightImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
rightImageView.image = [UIImage imageNamed:@"weixin"];
textField.rightView = rightImageView;
textField.rightViewMode = UITextFieldViewModeAlways;
//5.設置密文顯示
//textField.secureTextEntry = YES;
//代碼設置自動獲取焦點,如果不設置就沒有游標顯示,點擊之後才有顯示
[textField becomeFirstResponder];
//6.設置單詞字母是否大寫
/*
UITextAutocapitalizationTypeNone,沒有
UITextAutocapitalizationTypeWords, 每一個單詞首字母大寫
UITextAutocapitalizationTypeSentences, 每一個句子的首字母
UITextAutocapitalizationTypeAllCharacters,每個英文字母都大寫
*/
textField.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;
//7.是否糾錯
/*
UITextAutocorrectionTypeDefault,
UITextAutocorrectionTypeNo,
UITextAutocorrectionTypeYes,
*/
textField.autocorrectionType = UITextAutocorrectionTypeYes;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITextField * textField = (UITextField*)[self.view viewWithTag:1];
//失去焦點
[textField resignFirstResponder];
}
#pragma mark - 4.設置鍵盤
- (void)setkeyboard {
UITextField *textField = [self.view viewWithTag:1];
// 修改鍵盤的顏色
/*
UIKeyboardAppearanceDefault, // Default apperance for the current input method.
UIKeyboardAppearanceDark NS_ENUM_AVAILABLE_IOS(7_0),
UIKeyboardAppearanceLight NS_ENUM_AVAILABLE_IOS(7_0),
UIKeyboardAppearanceAlert = UIKeyboardAppearanceDark, // Deprecated
*/
textField.keyboardAppearance = UIKeyboardAppearanceDefault;
//鍵盤的樣式
textField.keyboardType = UIKeyboardTypeDefault;
// 設置inputview和inputAccessoryView
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
view.backgroundColor = [UIColor brownColor];
textField.inputView = view;
UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
view1.backgroundColor = [UIColor blueColor];
textField.inputAccessoryView = view1;
}