Set password protection in UITextView

前端 未结 3 1704
醉酒成梦
醉酒成梦 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:15

    Using an UITextView leaves the whole job of masking the text yourself. You also need to make sure you disable copying for security reasons. Set your delegate property and handle this something on these lines:

    var originalText: String?
    
    func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        originalText = ((originalText ?? "") as NSString).replacingCharacters(in: range, with: text)
        return true
    }
    
    func textViewDidChange(_ textView: UITextView) {
        textView.text = String(repeating: "*", count: (textView.text ?? "").count)
    }
    

    If you need to retrieve the value of the actual text that was input use the originalText property.

提交回复
热议问题