-2

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,259
  • 21
  • 28
  • 43
Dreiohc
  • 125
  • 1
  • 8
  • It is always on the end! can you show what you done until now? – swiftPunk 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.

Etienne Kaiser
  • 2,634
  • 8
  • 19
  • 29
  • 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. – Etienne Kaiser Sep 21 '21 at 08:27