UITextField基础语法

#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;

}

相关推荐