Getting UITextField cursor position in ios

I am trying to control the cursor position in a UITextField. Users cannot insert more than one character at a time in the middle of a text field. He moves it to the end of the text box. So this post in SO: Cursor Position in UITextField Solves my problem. But I need to know the current cursor position.

My code is as follows:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { if (textField.tag == 201) { [myclass selectTextForInput:textField atRange:NSMakeRange(idx, 0)]; } } 

This gives me an error on idx. How to find it?

+15
ios uitextfield
May 08 '13 at 3:54
source share
4 answers

UITextField follows the UITextInput protocol, which has methods for getting the current selection. But the methods are complicated. You need something like this:

 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { if (textField.tag == 201) { UITextRange *selRange = textField.selectedTextRange; UITextPosition *selStartPos = selRange.start; NSInteger idx = [textField offsetFromPosition:textField.beginningOfDocument toPosition:selStartPos]; [myclass selectTextForInput:textField atRange:NSMakeRange(idx, 0)]; } } 
+27
May 8 '13 at 4:04
source share

Quick version

 if let selectedRange = textField.selectedTextRange { let cursorPosition = textField.offsetFromPosition(textField.beginningOfDocument, toPosition: selectedRange.start) print("\(cursorPosition)") } 

My complete answer about getting and setting the cursor position is here .

+5
Jan 21 '16 at 11:24
source share

The code you posted will not work to determine where the cursor is. You need a get method, not a set. It should be something like:

 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { if (textField.tag == 201) { UITextRange selectedRange = [textField selectedTextRange]; // here you will have to check whether the user has actually selected something if (selectedRange.empty) { // Cursor is at selectedRange.start ... } else { // You have not specified home to handle the situation where the user has selected some text, but you can use the selected range and the textField selectionAffinity to assume cursor is on the left edge of the selected range or the other ... } } } 

For more information, check the UITextInput protocol http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UITextInput_Protocol/Reference/Reference.html#//apple_ref/occ/intf/UITextInput

Update: @rmaddy posted some nice extra bits that I missed in my answer - how to handle a text position from NSTextRange and convert NSTextPosition to int.

+3
May 08 '13 at 4:02
source share

Fast decision.

You can set the cursor padding by subclassing the UITextfield class and implementing these two methods.

Hope this helps someone in need.

 override func textRect(forBounds bounds: CGRect) -> CGRect { return bounds.inset(by: UIEdgeInsets.init(top: 4, left: 40, bottom: 1, right: 15)) } override func editingRect(forBounds bounds: CGRect) -> CGRect { return bounds.inset(by: UIEdgeInsets.init(top: 4, left: 40, bottom: 1, right: 15)) } 
0
Mar 27 '19 at 13:39
source share



All Articles