码迷,mamicode.com
首页 > 移动开发 > 详细

iOS-Swift-UItextField

时间:2019-11-18 18:34:05      阅读:94      评论:0      收藏:0      [点我收藏+]

标签:false   ali   ica   height   label   nts   edit   keyboard   ext   

//

//  ViewController.swift

//  Label

//

//  Created by 赵士军 on 2019/11/18.

//  Copyright © 2019 赵士军. All rights reserved.

//

 

import UIKit

 

class ViewController: UIViewController ,UITextFieldDelegate{

 

    override func viewDidLoad() {

        super.viewDidLoad()

 

        // Do any additional setup after loading the view.

        

        self.getTextField()

    }

    

  

    @objc func getTextField(){

        self.view.backgroundColor = .red

        

        let textField = UITextField.init(frame: CGRect(x: 0, y: 0, width: 200, height: 40))

        textField.placeholder = "请输入"

        textField .becomeFirstResponder()

        textField.textAlignment = .left

        textField.delegate=self

        textField.font = .systemFont(ofSize: 15)

        textField.center=self.view.center

        textField.keyboardType = .default

        textField.borderStyle = .roundedRect

        textField.clearButtonMode = .whileEditing

        textField.isSecureTextEntry = false

       

        textField.leftViewMode = .always

            textField.rightViewMode = .always

        let leftButton = UIButton(type: .infoDark)

        leftButton.frame = CGRect(x: 0, y: 0, width: 30, height: 30)

        textField.leftView = leftButton

        

        let rightButton = UIButton(type: .contactAdd)

        rightButton.frame = CGRect(x: 0, y: 0, width: 30, height: 30)

        textField.rightView = rightButton

        textField.keyboardAppearance = UIKeyboardAppearance.alert

        self.view .addSubview(textField)

        

    }

   override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {

    self.view .endEditing(true)

    }

    

    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 textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

        print("文本输入内容将要发生变化(每次输入都会调用)")

        return true

    }

    

    func textFieldShouldClear(_ textField: UITextField) -> Bool {

        print("将要清除输入内容,返回值是是否要清除掉内容")

        return true

    }

    

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {

        print("将要按下Return按钮,返回值是是否结束输入(是否失去焦点)")

        

        return true

    }

 

    /*

    // 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.destination.

        // Pass the selected object to the new view controller.

    }

    */

 

}

 

iOS-Swift-UItextField

标签:false   ali   ica   height   label   nts   edit   keyboard   ext   

原文地址:https://www.cnblogs.com/ZsjXxy/p/11883729.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!