Set password protection in UITextView

前端 未结 3 1707
醉酒成梦
醉酒成梦 2021-01-28 01:31

Can i hide password in UITextView by * or any other symbol? I need to use UITextView instead of UITextField. I want to hide all characters of textView.

3条回答
  •  说谎
    说谎 (楼主)
    2021-01-28 02:09

    Create a global variable for password string.

    var passwordString = ""
    

    Then set delegates of UITextView like:

    func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        passwordString = ((passwordString ?? "") as NSString).replacingCharacters(in: range, with: text)
        return true
    }
    
    func textViewDidChange(_ textView: UITextView) {
    //replace character with * or anyother character
        yourtextView.text = String(repeating: "*", count: (textView.text ?? "").count)
    }
    

    and dont forget to do this:

    yourTextview.delegate = self
    

提交回复
热议问题