UITextField的使用

//创建输入框

    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(80, 80, 180, 40)];

    //设置边框类型

    /*

     UITextBorderStyleNone,

     UITextBorderStyleLine,

     UITextBorderStyleBezel,

     UITextBorderStyleRoundedRect

     */

    textField.borderStyle = UITextBorderStyleRoundedRect;

    

    //设置字体的颜色

    textField.textColor = [UIColor greenColor];

    //设置对齐方式

    textField.textAlignment = NSTextAlignmentCenter;

    //设置字体的大小

    textField.font = [UIFont systemFontOfSize:20];

//    textField.font = [UIFont fontWithName:<#(NSString *)#> size:<#(CGFloat)#>]

    

    //设置首字母大小写,默认首字母大写

    /*

     UITextAutocapitalizationTypeNone,  关闭大写

     UITextAutocapitalizationTypeWords, 每一个单词的首字母大写

     UITextAutocapitalizationTypeSentences,

     UITextAutocapitalizationTypeAllCharacters, 所有字符大写

     */

    textField.autocapitalizationType = UITextAutocapitalizationTypeNone;

    

    //设置弹出键盘的样式

    //UIKeyboardTypeNumberPad  数字

    //UIKeyboardTypeEmailAddress  邮箱

    //UIKeyboardTypeURL  网址

//    textField.keyboardType = UIKeyboardTypeURL;

    

    //设置return的样式

    textField.returnKeyType = UIReturnKeyGo;

    

    //设置弹出键盘

    [textField becomeFirstResponder];

    

    //设置安全输入模式

//    textField.secureTextEntry = YES;

    

    //设置清楚按钮

    textField.clearButtonMode = UITextFieldViewModeWhileEditing;

    

    //设置代理

    textField.delegate = self;

    

    //添加到视图上显示

    [_window addSubview:textField];

    

    

    return YES;

}


#pragma mark - UITextFieldDelegate

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {


    NSLog(@"将要开始编辑");

    return YES;

}


- (void)textFieldDidBeginEditing:(UITextField *)textField {


    NSLog(@"已经开始编辑");

}


- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {

    NSLog(@"将要结束编辑");

    return YES;

}


- (void)textFieldDidEndEditing:(UITextField *)textField {


    NSLog(@"已经结束编辑");

}


//return被点击的时候执行的代理方法

- (BOOL)textFieldShouldReturn:(UITextField *)textField {


    NSLog(@"return被点击了");

    //收起键盘

    

    [textField resignFirstResponder];

    

    return YES;

    

}


//开始编辑的时候调用的方法

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {


    NSLog(@"开始编辑了");

    return YES;

    

}


猜你喜欢

转载自blog.csdn.net/Remember29/article/details/45216751