iOS--UITextField的简单使用

- (void)viewDidLoad {
    [super viewDidLoad];

    //定制textField
    //键盘风格
    self.userNameTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;//标准电话键盘
    //键盘外观
    self.userNameTextField.keyboardAppearance = UIKeyboardAppearanceLight;//浅色外观
    //回车键
    self.userNameTextField.returnKeyType = UIReturnKeyJoin;//可以不设置
    //首字母是否自动大写
    self.userNameTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;//不大写
    /*
     UITextAutocapitalizationTypeNone,
     UITextAutocapitalizationTypeWords,单词首字母大写
     UITextAutocapitalizationTypeSentences,句子首字母大写
     UITextAutocapitalizationTypeAllCharacters,搜友字母大写
     */
    //自动更正
    self.userNameTextField.autocorrectionType = UITextAutocorrectionTypeYes;
    //密文输入
    self.userNameTextField.secureTextEntry = NO;
    //设置边框样式
    self.userNameTextField.borderStyle = UITextBorderStyleRoundedRect;//圆角
    //设置清除模式
    self.userNameTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    //设置左右边框显示视图
     UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.userPassworldTextField.frame)/3.0, CGRectGetHeight(self.userPassworldTextField.frame))];
    nameLabel.text = @"用户名:";
    nameLabel.textColor = [UIColor blackColor];
    nameLabel.font = [UIFont systemFontOfSize:18];
    self.userNameTextField.leftView = nameLabel;
    self.userNameTextField.leftViewMode = UITextFieldViewModeAlways;
    //再次编辑是否清空
    self.userNameTextField.clearsOnBeginEditing = NO;
    //设置引用语
    self.userNameTextField.placeholder = @"请输入电话号码:";
    //设置文本信息
    self.userNameTextField.textColor = [UIColor blackColor];
    self.userNameTextField.font = [UIFont systemFontOfSize:18];
    //其他设置
    self.userNameTextField.layer.cornerRadius = 10;
    self.userNameTextField.layer.borderWidth = 1;
    self.userNameTextField.layer.borderColor = [UIColor lightGrayColor].CGColor;

    //设置代理
    self.userNameTextField.delegate = self;
    /*
    代理方法里边可以根据需要做很多处理,具体实现不在鳌述
    */
    //添加到视图界面
    [self.view addSubview:self.userNameTextField];
}

一般情况下都需要写上以下代码以取消第一响应

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.userNameTextField resignFirstResponder];
    [self.userPassworldTextField resignFirstResponder];
}
 协议方法:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    //当用户按下ruturn,把焦点从textField移开那么键盘就会消失了
    [textField resignFirstResponder];
    returnYES;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值