gpt4 book ai didi

ios - 单独部分的 secureTextEntry -- swift

转载 作者:行者123 更新时间:2023-11-28 12:21:57 28 4
gpt4 key购买 nike

我用 secureTextEntry = true 将标签中的字符串更改为“••••••••••••••”。它工作完美,但我如何更改整个字符串,除了字符串中的最后 4 个字符?

最佳答案

此处的情况取决于您如何使用此字符串:在 UILabel 中,还是在 UITextField 中。无论哪种方式,最好的办法是像这样扩展 String 来执行以下操作:

extension String {

init(withSecureShowOnlyLast4 values : String) {
self.init()
let array = [Character](values.characters)
if array.count <= 4 {
self = values
} else {
var first = array.count - 4
var lastFour : [Character] = []
while first <= (array.count - 1) {
lastFour.append(array[first])
first += 1
}
self = String(Array(repeating: "•", count: array.count - 4) + lastFour)
}
}

}



let pass : String = "fesnjufodpsk"

let obfs = String(withSecureShowOnlyLast4: pass) //prints : ••••••••dpsk

根据您的需要,这就是我使用它的方式:

UILABEL:

这是一个自定义类,可确保安全地保存您想要的数据。

class SafeLabel : UILabel {

var makeSafe : Bool = false
private var safeKey : String?
private var alternateSet = Bool()

override var text: String? {
didSet {
if makeSafe && !alternateSet {
alternateSet = true
safeKey = text
self.text = nil
} else if alternateSet {
alternateSet = false
}
}

willSet {
if makeSafe && !alternateSet {
self.safeKey = text
}
}
}

var safe : String {
get {
guard let sa = safeKey else {
return ""
}
guard makeSafe else {
return sa
}
return String(withSecureShowOnlyLast4: sa)
}
}
}
let lab = SafeLabel()

lab.makeSafe = true

lab.text = "9j3od3dkuhosfg"

print(lab.safe) //prints ••••••••••osfg

请注意 .makeSafe 的使用,它会一起删除数据的打印和使用(而不是删除它)。

对于 UITEXTFIELD:

对于此类,我将使用 textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool 方法来修改字符串。在这里,我将像这样子类化 TextField,并像这样更改 protected 字符串:

class SafeField : UITextField {

var makeSafe : Bool = false
private var safeKey : String?
private var alternateSet = Bool()

override var text: String? {
didSet {
if makeSafe && !alternateSet {
alternateSet = true
safeKey = text
self.text = nil
} else if alternateSet {
alternateSet = false
}
}

willSet {
guard text != nil else {
if let t = safeKey, t.characters.count == 1 || t.isEmpty {
self.safeKey = nil
} else {
isDeletingEntry()
}
return
}
if makeSafe && !alternateSet, let t = text {
if let s = safeKey {
self.safeKey = s + t
} else {
self.safeKey = t
}
}
}
}

var safe : String {
get {
guard let sa = safeKey else {
return ""
}
guard makeSafe else {
return sa
}
return String(withSecureShowOnlyLast4: sa)
}
}

override init(frame: CGRect) {
super.init(frame: frame)
}

private func isDeletingEntry() {
if let old = safeKey {
let new = String(old.characters.dropLast())
self.text = new
}
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}


class View: UIView, UITextFieldDelegate {
//Of course, this can be a ViewController, or anything that can handle the
//code bellow
var safe : SafeField = {
var s = SafeField()
s.makeSafe = true
return s
}()

override init(frame: CGRect) {
super.init(frame: frame)
addSubview(safe)
safe.frame = .zero
safe.delegate = self
}

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

guard safe != textField else {

if string.characters.isEmpty {
safe.text = nil
} else {
safe.text = string
}

return false
}

return true
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

}

显然,在检索值时,您需要使用 safe 而不是 text,因为 text 总是硬编码为 nil,考虑到混淆标签在 UI 开发中总是少数,我认为这应该不是问题。要从 TextField 获取值,您需要将 makeSafe 变量设置为 false 以检索字符串的未混淆值。以安全为主要焦点,这里的优点是您可以设置代码以根据需要在任何地方设置和取消设置 makeSafe 变量,而不必担心某些东西或某人直接从另一个文件访问它的值。

当然,如果您使用键盘元素或某种协议(protocol)直接修改 UILabel,只需将 SafeLabel 的文本变量替换为 SafeField 中的文本变量。 (如果您使用的是自定义协议(protocol),请确保通过可选转换检查标签确实是 SafeLabel :

    func protocolMethod(label: UILabel, doSomething: Bool) {
guard let lab = label as? SafeLabel else {
//do your stuff
return
}
//Here you can manipulate lab as a SafeLabel and modify it according to the SafeLabel SubClass
}

关于ios - 单独部分的 secureTextEntry -- swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44526375/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com