gpt4 book ai didi

swift - 用户不应该能够删除文本字段中给定的符号(文本)

转载 作者:行者123 更新时间:2023-11-30 10:04:06 33 4
gpt4 key购买 nike

enter image description here

func textField(textField: UITextField,shouldChangeCharactersInRange range: NSRange,replacementString string: String) -> Bool {返回真}

我必须限制用户快速从文本字段中删除此符号。用户可以从文本字段中删除任何内容,而无需删除此符号。

最佳答案

由于最可能的情况是欧元符号将始终位于文本字段中并且将是第一个字符,因此我会检查要更改的字符范围是否长于 0 并从 0 开始。(无论怎样,这都会起作用)第一个位置的符号)

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
if range.length>0 && range.location == 0 {
return false
}
return true
}

但是,如果欧元符号并不总是在文本字段中,您可以通过获取对用户正在更改的字符串的引用并检查其是否包含欧元符号来检查用户是否正在删除该符号:

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
if range.length>0 && range.location == 0 {
let changedText = NSString(string: textField.text!).substringWithRange(range)
if changedText.containsString("€") {
return false
}
}
return true
}

关于swift - 用户不应该能够删除文本字段中给定的符号(文本),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37030040/

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