Swift_UITextField

2016-10-10  本文已影响5138人  _杜兜兜_

1,提示信息

textField.placeholder = "请输入"

2,密文输入

textField.secureTextEntry = true

3,不可被输入

textField.userInteractionEnabled = false

4,限制输入的长度
调用

       textField.addTarget(self, action: #selector(self.textDidChangeMax(_:)), forControlEvents: UIControlEvents.EditingChanged)

声明方法

    var NumberMax : Int{
        return 11
    }
    func textDidChangeMax(sender:UITextField) {
        let lang = textInputMode?.primaryLanguage
        if lang == "zh-Hans" {
            let range = textField.markedTextRange
            if range == nil {
                if textField.text?.characters.count >= NumberMax {
                    textField.text = textField.text?.substringToIndex((textField.text?.startIndex.advancedBy(NumberMax))!)
                }
            }
        }
        else {
            if textField.text?.characters.count >= NumberMax {
                textField.text = textField.text?.substringToIndex((textField.text?.startIndex.advancedBy(NumberMax))!)
            }
        }
    }
上一篇下一篇

猜你喜欢

热点阅读