gpt4 book ai didi

ios - 将文本设置为 shouldChangeCharactersIn 内的 UITextField 后,光标将结束

转载 作者:可可西里 更新时间:2023-11-01 00:54:58 28 4
gpt4 key购买 nike

我有文本标签,里面有电话号码。我在用户输入时屏蔽电话号码,以便在 shouldChangeCharactersIn 函数中;

  • 我得到用户输入(字符串)
  • 将该输入添加到已在 UITextField 中编写的文本中
  • 屏蔽文本并将其设置为 UITextField
  • 返回假

我的问题是,在我设置 UITextfield 的文本(删除或添加新字符 UITextField)后,光标移动到末尾,但我希望它保持在同一位置.(意思是相同的位置,我的意思是当我不实现 shouldChangeCharactersIn 函数时相同)我该怎么做?谢谢。

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

guard CharacterSet(charactersIn: "0123456789").isSuperset(of: CharacterSet(charactersIn: string)) else {
return false
}
if let text = textField.text {
let newLength = text.count + string.count - range.length
let newText = text + string

let textFieldText: NSString = (textField.text ?? "") as NSString
let txtAfterUpdate = textFieldText.replacingCharacters(in: range, with: string)
if(newLength <= 15){
//textField.text = txtAfterUpdate
textField.text = txtAfterUpdate.digits.applyPatternOnNumbers()
return false
}
return newLength <= 15
}
return true
}

蒙版功能:

func applyPatternOnNumbers(pattern: String = "(###) ### ## ##", replacmentCharacter: Character = "#") -> String {
var pureNumber = self.replacingOccurrences( of: "[^0-9]", with: "", options: .regularExpression)
for index in 0 ..< pattern.count {
guard index < pureNumber.count else { return pureNumber }
let stringIndex = String.Index(encodedOffset: index)
let patternCharacter = pattern[stringIndex]
guard patternCharacter != replacmentCharacter else { continue }
pureNumber.insert(patternCharacter, at: stringIndex)
}
return pureNumber
}

我想要的 GIF

enter image description here

最佳答案

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

guard CharacterSet(charactersIn: "0123456789").isSuperset(of: CharacterSet(charactersIn: string)) else {
return false
}
if let text = textField.text {
let newLength = text.count + string.count - range.length
let newText = text + string

let textFieldText: NSString = (textField.text ?? "") as NSString
let txtAfterUpdate = textFieldText.replacingCharacters(in: range, with: string)
if(newLength <= 15){

var currentPosition = 0
if let selectedRange = textField.selectedTextRange {
currentPosition = textField.offset(from: textField.beginningOfDocument, to: selectedRange.start)
}

if(string == ""){
if(currentPosition == 2){
if(textField.text!.count > 2){
currentPosition = 1
}else{
currentPosition = 0
}
}else if(currentPosition == 7){
currentPosition = 4
}else if(currentPosition == 11){
currentPosition = 9
}else if(currentPosition == 14){
currentPosition = 12
}else{
if(currentPosition != 1){
currentPosition = currentPosition - 1
}
}
}else{
if(currentPosition == 0){
currentPosition = 2
}else if(currentPosition == 4){
currentPosition = 7
}else if(currentPosition == 9){
currentPosition = 11
}else if(currentPosition == 12){
currentPosition = 14
}else{
currentPosition = currentPosition + 1
}
}

textField.text = txtAfterUpdate.applyPatternOnNumbers()
print("textField Length -> : \(textField.text?.count ?? 0)")

if let newPosition = textField.position(from: textField.beginningOfDocument, offset: currentPosition) {
textField.selectedTextRange = textField.textRange(from: newPosition, to: newPosition)
}

return false
}
return newLength <= 15
}
return true
}

关于ios - 将文本设置为 shouldChangeCharactersIn 内的 UITextField 后,光标将结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51784571/

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