IOS控件-UITextField文本框控件的基本使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_21153627/article/details/84070038

首先视图要继承UITextFieldDelegate

//UITextField文本框控件的基本使用
    func test5() {
        let textField = UITextField(frame: CGRect(x: 60, y: 80, width: 200, height: 30))
        //设置文本框的边框对象为圆角方式
        textField.borderStyle=UITextBorderStyle.roundedRect;
        //设置文本框内容的预设值
        textField.placeholder="your Email"
        //关闭文本框的语法错误提示功能
        textField.autocorrectionType=UITextAutocorrectionType.no;
        //设置输入文字时键盘上 回车的类型
        textField.returnKeyType=UIReturnKeyType.done
        //设置文本框右侧的删除按钮只在编辑时显示
        textField.clearButtonMode=UITextFieldViewMode.whileEditing;
        //设置文本框对象的键盘类型为系统提供的邮箱地址类型
        textField.keyboardType=UIKeyboardType.emailAddress;
        //设置对应键盘为暗色主题
        textField.keyboardAppearance=UIKeyboardAppearance.dark;
        //设置文本框的代理为当前控制器
        textField.delegate = self;
        self.view.addSubview(textField);
        
        
    }
    
    //添加一个代理方法 当按下回车时调用
    func textFieldShouldReturn(_ textFileld:UITextField) -> Bool  {
        //点击时。失去焦点隐藏键盘
        textFileld.resignFirstResponder();
        return true;
    }

猜你喜欢

转载自blog.csdn.net/qq_21153627/article/details/84070038