iOS UI入门——使用Objective-C和Swift简单实现UITextField

Objective-C代码:

  • 设置代理:
@interface ViewController ()<UITextFieldDelegate>
  • UITextField相关设置:
-(void)setupTextField{
    //初始化
    UITextField * testTextField = [[UITextField alloc] initWithFrame:CGRectMake(30, 200, self.view.frame.size.width - 60, 50)];
    //设置圆角
    testTextField.layer.cornerRadius = 6;
    testTextField.layer.masksToBounds = YES;
    //设置描边
    testTextField.layer.borderColor = [UIColor lightGrayColor].CGColor;
    testTextField.layer.borderWidth = 1;
    //设置键盘类型
    testTextField.keyboardType = UIKeyboardTypeNumberPad;
    //设置键盘的回车键
    testTextField.returnKeyType = UIReturnKeySend;
    //设置为密文,为NO的话则为明文
    testTextField.secureTextEntry = YES;
    //设置代理
    testTextField.delegate = self;
    //设置提示语
    testTextField.placeholder = @"请输入手机号";
    //设置提示语的颜色
    [testTextField setValue:[UIColor greenColor] forKeyPath:@"_placeholderLabel.textColor"];
    //设置字体大小
    testTextField.font = [UIFont systemFontOfSize:16];
    //设置字体颜色
    testTextField.textColor = [UIColor redColor];
    //添加textField
    [self.view addSubview:testTextField];
}
  • UITextField相关代理:
-(BOOL)textFieldShouldClear:(UITextField *)textField{
    //点击清除按钮时调用
    return YES;
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    //点击回车键时调用
    return YES;
}

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    //将要编辑时调用
    return YES;
}

-(void)textFieldDidBeginEditing:(UITextField *)textField{
    //开始编辑时调用
}

-(BOOL)textFieldShouldEndEditing:(UITextField *)textField{
    //将要结束编辑时调用
    return YES;
}

-(void)textFieldDidEndEditing:(UITextField *)textField{
    //结束编辑时调用
}

-(void)textFieldDidEndEditing:(UITextField *)textField reason:(UITextFieldDidEndEditingReason)reason{
    // if implemented, called in place of textFieldDidEndEditing:
}

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    //textField内容发生改变时调用
    return YES;
}

Swift代码:

  • 设置代理:
class ViewController: UIViewController,UITextFieldDelegate
  • UITextField相关设置:
    func setupTextField() {
        //初始化
        let testTextField = UITextField.init(frame: CGRect.init(x: 30, y: 200, width: self.view.frame.size.width - 60, height: 50))
        //设置圆角
        testTextField.layer.cornerRadius = 6
        testTextField.layer.masksToBounds = true
        //设置描边
        testTextField.layer.borderColor = UIColor.lightGray.cgColor
        testTextField.layer.borderWidth = 1
        //设置键盘类型
        testTextField.keyboardType = UIKeyboardType.numberPad
        //设置键盘的回车键
        testTextField.returnKeyType = UIReturnKeyType.done
        //设置为密文,为NO的话则为明文
        testTextField.isSecureTextEntry = true
        //设置代理
        testTextField.delegate = self
        //设置提示语
        testTextField.placeholder = "请输入手机号"
        //设置提示语的颜色
        testTextField.setValue(UIColor.green, forKeyPath: "_placeholderLabel.textColor")
        //设置字体大小
        testTextField.font = UIFont.systemFont(ofSize: 16)
        //设置字体颜色
        testTextField.textColor = UIColor.red
        //添加textField
        self.view.addSubview(testTextField)
    }
  • UITextField相关代理:
    func textFieldShouldClear(_ textField: UITextField) -> Bool {
        //点击清除按钮时调用
        return true
    }

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        //点击回车键时调用
        return true
    }

    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        //将要编辑时调用
        return true
    }

    func textFieldDidBeginEditing(_ textField: UITextField) {
        //开始编辑时调用
    }

    func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
        //将要结束编辑时调用
        return true
    }

    func textFieldDidEndEditing(_ textField: UITextField) {
        //结束编辑时调用
    }

    func textFieldDidEndEditing(_ textField: UITextField, reason: UITextFieldDidEndEditingReason) {
        // if implemented, called in place of textFieldDidEndEditing:
    }

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        //textField内容发生改变时调用
        return true
    }

猜你喜欢

转载自blog.csdn.net/aaaaazq/article/details/80829297