UITextFieldDelegate

//  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))

    override func 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 代表输入的新字符

        return true

        

    }

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

    func textFieldDidBeginEditing(_ textField: UITextField) {

        print("didbegin")

    }

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

    func textFieldDidEndEditing(_ textField: UITextField) {

        print("didend")

    }

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

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

        return true

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

    }

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

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

        return true

    }

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

        return true

    }

    //点击屏幕收键盘

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

        textField.resignFirstResponder()

    }

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }



}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值