0

I want to put cursor (indicator point) to the end of text field.

I have the following code, but it does nothing

textField.selectedTextRange = textField.textRange(from: textField.endOfDocument, to: textField.endOfDocument)

func textFieldDidBeginEditing(_ textField: UITextField) {
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: "selection_sub"), object: nil)
        imitateSingleSelection()
        textField.text = textField.text?.toLocalNumberFormatFromServer()
        textField.selectedTextRange = textField.textRange(from: textField.endOfDocument, to: textField.endOfDocument)
    }

Is there a way to put cursor to the end of text field?

This is my code

This is my TextField

Daria
  • 1
  • 1
  • try textField.becomeFirstResponder() to give focus to the text field and make the keyboard appear – vishnu anilkumar Aug 30 '21 at 10:42
  • I tried it. I also tried this code in the same place textField.selectedTextRange = textField.textRange(from: textField.beginningOfDocument, to: textField.endOfDocument) And it works - selects all text in text field. It seems to me selectedTextRange doesn't work if range has zero length – Daria Aug 30 '21 at 11:06
  • check out this link. https://stackoverflow.com/questions/34922331/getting-and-setting-cursor-position-of-uitextfield-and-uitextview-in-swift – flutterloop Aug 30 '21 at 14:19
  • I tried it - doesn't work for me. – Daria Aug 31 '21 at 20:04
  • Please add a snipet of your code and the textfield or the responder you are trying to use this on for a better idea – Anjula Serasinghe Sep 01 '21 at 13:04
  • Please provide enough code so others can better understand or reproduce the problem. – Community Sep 01 '21 at 13:04

0 Answers0