0

Is there any other way to do this?

EBDOKUM
  • 1,696
  • 3
  • 15
  • 33
  • `UITextField` has never (publicly) had a `setSelectedRange:` method. `UITextView` has a `setSelectedRange:` method (in Objective-C). Are you trying to set the selected range of a `UITextField` or of a `UITextView`? – rob mayoff Jan 11 '16 at 15:17
  • Possible duplicated to http://stackoverflow.com/questions/34412115/limit-length-of-multiple-uitextfields-in-swift-2 – Twitter khuong291 Jan 11 '16 at 15:18
  • @robmayoff true, might be so, but still I need this to work on UITextField – EBDOKUM Jan 11 '16 at 16:16
  • @EBDOKUM hello why do you want to move cursor. do you want user to start typing from certain position? – hellosheikh Jan 11 '16 at 16:20
  • @hellosheikh I just want to keep "%" symbol after the number that the user enters at all times – EBDOKUM Jan 11 '16 at 17:30
  • oh okay so you have a textfield filled with default value "%" and when user start types it'll be like this "hello%" right ? @EBDOKUM – hellosheikh Jan 11 '16 at 17:45
  • @hellosheikh I added a Target for all Editing events - it calls a method, which takes the text inside of the field, and puts the "%" symbol to the end. But that doesn't work that great because if the cursor gets behind this last symbol, there is no way to delete it and therefore I have to move it manually. And that is exactly what happens when I first tap the field. What I wanted to do is to add a method, which would always put the cursor second from the end. – EBDOKUM Jan 11 '16 at 19:08

1 Answers1

1

UITextField conforms to UITextInput. This means that you can set its selectedTextRange property (https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextInput_Protocol/index.html#//apple_ref/occ/intfp/UITextInput/selectedTextRange). A range with zero length is the cursor alone.

matt
  • 515,959
  • 87
  • 875
  • 1,141