Cursor goes to end after setting text to UITextField inside shouldChangeCharactersIn

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Cursor goes to end after setting text to UITextField inside shouldChangeCharactersIn



I have text label which has phone number in it. I mask the phone number when user is typing so that in shouldChangeCharactersIn function;


shouldChangeCharactersIn


UITextField


UITextField



My question is that after I set text of UITextfield (delete or add new character UITextField, cursor moves to the end but I want it to stay in the same position. (By meaning same position, I mean same when I don't implement shouldChangeCharactersIn function) How can I do that? Thank you.


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



Mask Function:


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



What I want in GIF



enter image description here




1 Answer
1


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





@Emre Önder check it May be help you
– Tm Goyani
Aug 14 at 10:16





When I make it return true, It adds additional character because we already set new text with textField.text =
– Emre Önder
Aug 14 at 12:59





@EmreÖnder Check my updated code. it will be work as par you want. Thanks.
– Tm Goyani
Aug 16 at 9:57





This doesn't mask the phone Number?
– Emre Önder
Aug 16 at 9:58





@EmreÖnder please check one more time it will be done
– Tm Goyani
Aug 16 at 10:41






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

make 2 or more post in bootsrap

Store custom data using WC_Cart add_to_cart() method in Woocommerce 3

Firebase Auth - with Email and Password - Check user already registered