UITextFieldDelegate

来源:互联网 发布:python多进程读取文件 编辑:程序博客网 时间:2024/05/21 05:20

//  UITextField

//

//  Created by Catherine on 2017/8/29.

//  Copyright © 2017 Catherine. All rights reserved.

//


import UIKit


class ViewController:UIViewController ,UITextFieldDelegate{

        let textField:UITextField =UITextField(frame: CGRect(x: 20, y:100, width: 280, height:40))

    overridefunc viewDidLoad() {

        super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib.


        //设置输入框风格为线性

        textField.borderStyle =UITextBorderStyle.line

        //有凹陷下去的感觉

        textField.borderStyle =UITextBorderStyle.bezel

        //===提示文字

        //设置输入框的提示文字

        textField.placeholder ="请输入文字"

        //===输入文字

        //设置输入的文字颜色

        textField.textColor =UIColor.red

        //设置文字的字体

        textField.font =UIFont.italicSystemFont(ofSize:15)

        //设置文字的对齐方式

        textField.textAlignment =NSTextAlignment.center

        //是否每次点编辑框都清空输入框中的文字

        textField.clearsOnBeginEditing =true

        textField.backgroundColor =UIColor.magenta

        //是否字体大小自适应 字多于框大小时字会变小

        textField.adjustsFontSizeToFitWidth =true

        //设置输入框为无效

        //textField.isEnabled = false

        //设置输入框的左视图

        let viewLeft:UIView =UIView(frame: CGRect(x:0, y: 0, width:40, height: 40))

        let viewRight:UIView =UIView(frame: CGRect(x:0, y: 0, width:40, height: 40))

        viewLeft.backgroundColor =UIColor.blue

        viewRight.backgroundColor =UIColor.brown

        textField.leftView = viewLeft

        //textField.rightView = viewRight

        //设置左右视图的显示模式  ==  whileEditing等一些模式

        textField.leftViewMode = .always

        textField.rightViewMode = .always

        //设置弹出的交互键盘

        /*8.0的模拟器中默认是xcode使用电脑键盘作为外接键盘,不再弹出虚拟键盘。如果设置了文本框的inputAccessoryView,那么只会弹出inputAccessoryView,而不会弹出键盘。

         */

        let board:UIView =UIView(frame: CGRect(x:0, y: 0, width:320, height: 200))

        board.backgroundColor =UIColor.orange

        //textField.inputView = board

        //设置附键盘视图

        textField.inputAccessoryView = board

        textField.delegate =self

        //显示清除按钮(由下方delegate shouldclear 返回值决定是否能触发)

        textField.clearButtonMode =UITextFieldViewMode.always

        self.view.addSubview(textField)

        

    }

    //对输入文字类型的控制

    func textField(_ textField:UITextField, shouldChangeCharactersIn range:NSRange, replacementString string:String) -> Bool {

        print(string)

     //string 代表输入的新字符

        returntrue

        

    }

    //已经进入编辑状态时调用的方法

    func textFieldDidBeginEditing(_ textField:UITextField) {

        print("didbegin")

    }

    //已经结束编辑状态时调用的方法

    func textFieldDidEndEditing(_ textField:UITextField) {

        print("didend")

    }

    //将要进入编辑状态时调用的方法

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

        returntrue

        //负责无法进入编辑状态

    }

    //将要结束编辑状态调用的方法

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

        returntrue

    }

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

        returntrue

    }

    //点击屏幕收键盘

    overridefunc touchesBegan(_ touches:Set<UITouch>, with event:UIEvent?) {

        textField.resignFirstResponder()

    }

    overridefunc didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }



}

原创粉丝点击