淘先锋技术网

首页 1 2 3 4 5 6 7
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, width: 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.
    }
    */

}

 

转载于:https://www.cnblogs.com/xujiahui/p/6952286.html