UITextField常见属性及方法

    /*************UITextField**************/
    //实例化
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 100, 280, 40)];
    //相关属性
//    textField.backgroundColor = [UIColor grayColor];
    //边框状态:borderStyle
    /*
     1、UITextBorderStyleRoundedRect  圆角边框
     2、UITextBorderStyleNone    无边框
     3、UITextBorderStyleLine   直角边框  黑色
     4、UITextBorderStyleBezel  直角边框  灰色
     */
    textField.borderStyle = UITextBorderStyleRoundedRect;
    //设置字体大小的属性:font
    textField.font = [UIFont systemFontOfSize:18.0];
    //设置字体颜色:textColor
    textField.textColor = [UIColor redColor];
    //对齐方式:textAlignment   左右对齐
    textField.textAlignment = NSTextAlignmentLeft;
    //对齐方式:contentVerticalAlignment  上下对齐  默认居中
    /*
     1、UIControlContentVerticalAlignmentTop  上对齐
     2、UIControlContentVerticalAlignmentCenter  居中
     3、UIControlContentVerticalAlignmentBottom  下对齐
     */
    textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    //自适应文字大小:adjustsFontSizeToFitWidth
    textField.adjustsFontSizeToFitWidth = YES;
    //最小字体的设置
    textField.minimumFontSize = 18.0;
    //设置背景图片:background   当边框为圆角时,图片不显示
    textField.background = [UIImage imageNamed:@"map.png"];
    //提示文字:placeholder
    textField.placeholder = @"请输入文字";
    //密文效果:secureTextEntry  默认为NO   YES:密文  NO:无密文
    textField.secureTextEntry = NO;
    //默认文字:text
    textField.text = @"1111111";
    //输入框是否可用:enabled  默认YES  YES:可用  NO:不可用
    textField.enabled = YES;
    //右下角return键的类型:returnKeyType
    /*
     1、UIReturnKeyDefault  return
     2、UIReturnKeyDone     Done
     3、UIReturnKeyEmergencyCall  紧急呼叫
     4、UIReturnKeyGoogle   search
     5、UIReturnKeyNext  next
     .
     .
     .
     */
    textField.returnKeyType = UIReturnKeyNext;
    //键盘类型:keyboardType
    /*
     1、UIKeyboardTypeDefault  中英文切换  符号
     2、UIKeyboardTypeNumberPad   数字键盘
     3、UIKeyboardTypeEmailAddress   @ .
     4、UIKeyboardTypeWebSearch  . Go
     5、UIKeyboardTypeNamePhonePad  英文中文数字
     6、UIKeyboardTypeURL  / .com
     .
     .
     .
     */
    textField.keyboardType = UIKeyboardTypeDefault;
    //键盘颜色  默认白色 :keyboardAppearance
    /*
     1、UIKeyboardAppearanceDark  黑色  夜间模式
     2、UIKeyboardAppearanceAlert  黑色  夜间模式
     3、UIKeyboardAppearanceDefault 白色  日间模式
     4、UIKeyboardAppearanceLight  白色  日间模式
     */
    textField.keyboardAppearance = UIKeyboardAppearanceLight;
    //字母大写:autocapitalizationType
    /*
     1、UITextAutocapitalizationTypeAllCharacters 所有字母都大写
     2、UITextAutocapitalizationTypeNone  所有字母都不会大写
     3、UITextAutocapitalizationTypeSentences  每个句子的首字母大写
     4、UITextAutocapitalizationTypeWords  每个单词的首字母大写
     */
    textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
    //是否自动纠错:autocorrectionType
    /*
     1、UITextAutocorrectionTypeDefault  默认
     2、UITextAutocorrectionTypeYes 纠错
     3、UITextAutocorrectionTypeNo  不纠错
     */
    textField.autocorrectionType = UITextAutocorrectionTypeNo;
    //一键删除的按钮:clearButtonMode
    /*
     1、UITextFieldViewModeAlways  一直存在
     2、UITextFieldViewModeNever   一直不出现
     3、UITextFieldViewModeWhileEditing  编辑时存在,结束编辑时消失
     4、UITextFieldViewModeUnlessEditing  编辑时消失,结束编辑时存在
     */
    textField.clearButtonMode = UITextFieldViewModeAlways;
    //再次编辑时清空之前的文字:clearsOnBeginEditing
    textField.clearsOnBeginEditing = YES;
    //设置左视图:leftView
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
    view.backgroundColor = [UIColor redColor];
    textField.leftView = view;
    //左视图出现的状态:leftViewMode  默认永不出现
    textField.leftViewMode = UITextFieldViewModeAlways;
    //右视图
    UIView *view2 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
    view2.backgroundColor = [UIColor orangeColor];
//    textField.rightView = view2;
    //右视图出现的状态
//    textField.rightViewMode = UITextFieldViewModeAlways;
    //左右视图同时出现时,不能使用同一个view,右视图出现时,一键删除的按钮是不存在的
   
    //判断textField是否正处于编辑的状态
    NSLog(@"%d",textField.editing);
    //获取textField上面的文字
    NSLog(@"%@",textField.text);
   
    /*************!!!delegate!!!**************/
    textField.delegate = self;
   
    //输入框开始第一响应,键盘出现
    [textField becomeFirstResponder];

    //添加到父视图上
    [self.view addSubview:textField];
}
#pragma mark - UITextFieldDelegate
//右下角return键的方法
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    //输入框失去第一响应,键盘消失
    [textField resignFirstResponder];
    //输入框开始第一响应,键盘出现
//    [textField becomeFirstResponder];
    return YES;
}
//输入框右边一键删除的按钮的方法
- (BOOL)textFieldShouldClear:(UITextField *)textField{
    //返回YES,一键删除有效;返回NO,一键删除无效
    if ([textField.text isEqualToString:@"123"]) {
        return YES;
    }
    return NO;
}
//输入框是否可以开始编辑
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    //返回YES,输入框可以开始编辑;返回NO,输入框不可编辑
    return YES;
}
//输入框是否可以结束编辑
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
    //返回YES,输入框可以结束编辑;返回NO,输入框不可结束编辑
    return YES;
}
//输入框开始响应就调用
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    textField.text = @"beginEditing";
}
//输入框失去响应就调用
- (void)textFieldDidEndEditing:(UITextField *)textField{
    textField.text = @"endEditing";
}

 

posted @ 2015-08-25 21:42  谢涵..Fting!  阅读(543)  评论(0编辑  收藏  举报