• Swift3.0 UITextField


    import UIKit
    
    private var textfieldd = UITextField()
    class TextFieldViewController: UIViewController,UITextFieldDelegate {
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            // Do any additional setup after loading the view.
            
            //设置大小
            textfieldd.frame = CGRect(x: 0, y: 100,  300, height: 50)
            
             // 设置 样式 (.none 无边框  .line 直线边框  .roundedRect 圆角矩形边框  .bezel 边线+阴影)
            textfieldd.borderStyle = .roundedRect
            
            //提示字
            textfieldd.placeholder = "提示字"
            textfieldd.textColor = UIColor.blue
            textfieldd.font = UIFont.boldSystemFont(ofSize: 20)
            textfieldd.textAlignment = .right
           
            // 设置 文字超出文本框时自适应大小
            textfieldd.adjustsFontSizeToFitWidth = true
            // 设置 最小可缩小的字号
            textfieldd.minimumFontSize = 14
            
            //清理按钮
            textfieldd.clearButtonMode = .whileEditing
            
            //键盘样式
            textfieldd.keyboardType = .default
            
            textfieldd.delegate = self
            
            
            
            self.view.addSubview(textfieldd)
            
        }
        
        
        ////////////////////////////////代理方法////////////////////////////////////
        // 输入框询问是否可以编辑 true 可以编辑  false 不能编辑
        func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
            print("我要开始编辑了...")
            return true
        }
        // 该方法代表输入框已经可以开始编辑  进入编辑状态
        func textFieldDidBeginEditing(_ textField: UITextField) {
            print("我正在编辑状态中...")
        }
        // 输入框将要将要结束编辑
        func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
            print("我即将编辑结束...")
            return true
        }
        // 输入框结束编辑状态
        func textFieldDidEndEditing(_ textField: UITextField) {
            print("我已经结束编辑状态...")
        } // 文本框是否可以清除内容
        func textFieldShouldClear(_ textField: UITextField) -> Bool {
            return true
        }
        // 输入框按下键盘 return 收回键盘
        func textFieldShouldReturn(_ textField: UITextField) -> Bool {
            textField.resignFirstResponder()
            return true
        }
        // 该方法当文本框内容出现变化时 及时获取文本最新内容
        func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
            
            return true
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
        
    
        /*
        // MARK: - Navigation
    
        // In a storyboard-based application, you will often want to do a little preparation before navigation
        override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            // Get the new view controller using segue.destinationViewController.
            // Pass the selected object to the new view controller.
        }
        */
    
    }
  • 相关阅读:
    巴菲特最推崇的10本书
    如何锻炼剑术基本功
    Ubuntu 20.04 LTS, CUDA 11.2.0, NVIDIA 455 and libcudnn 8.0.4
    缘起性空
    Mac 每次都要执行source ~/.bash_profile 配置的环境变量才生效
    Calcite分析 -- Register
    go超时控制有4种写法,你知道吗?
    npm install node-sass报错处理
    IDEA + maven热部署及自动编译不生效问题
    1-STM32+CH395Q(以太网)远程升级篇(自建物联网平台)-STM32通过ch395使用http下载程序文件,升级程序(单片机程序轮训检查更新)
  • 原文地址:https://www.cnblogs.com/xujiahui/p/6952286.html
Copyright © 2020-2023  润新知