在UITextView中设置密码保护 [英] Set password protection in UITextView

查看:103
本文介绍了在UITextView中设置密码保护的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以用*或任何其他符号在UITextView中隐藏密码吗?我需要使用UITextView而不是UITextField.我想隐藏textView的所有字符.

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.

推荐答案

使用UITextView可以自己屏蔽文本.您还需要确保出于安全原因禁用复制.设置您的delegate属性,并在以下几行中对其进行处理:

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

如果需要检索输入的实际文本的值,请使用originalText属性.

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

这篇关于在UITextView中设置密码保护的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆