0

I have created a textfield in Swift UI and I'm looking for a way to make the cursor of the textfield on editing to be at the end of the text even if I tapped the first letter on the textfield.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Dreiohc
  • 317
  • 2
  • 12
  • It is always on the end! can you show what you done until now? – ios coder Sep 20 '21 at 13:36
  • Yes it should always be at the end or at least stop the cursor from selecting the first text. I cannot do anything because on my search it's always UITextField. What I thought of is making a UIView and moving the text far from the left so the first text will not be selected – Dreiohc Sep 21 '21 at 01:28

1 Answers1

1

I think you mean something like this, to set .endOfDocument:

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

Now it will stay at the end of the position. If you search something more advanced, I recommend you this post and its answer.

Ole Pannier
  • 3,208
  • 9
  • 22
  • 33
  • 1
    This seems to be in UIKit solution, was hoping if there is one in SwiftUI but it looks like there is none. Thanks for this – Dreiohc Sep 21 '21 at 01:27
  • You are welcome! The second reference is a SwiftUI solution. Try that out. Positive response about that. – Ole Pannier Sep 21 '21 at 08:27