Can I set the insertion point on a UITextField

I have a UITextField that I want to present to a user with a pre-filled suffix. Obviously, I would like the insertion point to be at the beginning of the pre-filled text.

It doesn't seem obvious to me how to do this (there is no insertionPoint property), but is there a tricky way to do this?

+4
source share
4 answers

Override drawTextInRect: always draw your suffix, even if it is not contained in UITextFields text (use the temp line, which takes textfield.text, adds your suffix and draws it). After user editing is completed, add your suffix to the line (or use them separately if this helps).

+4
source

Everyone likes the 9 year old question.

UITextField corresponds to UITextInput . Thus, all the methods you are looking for in the class documentation are not found anywhere.

In my example, the line "$1234.00" should have been displayed, but only the range 1234 could edit.

 textField.delegate = self textField.text = "$\(myIntValue).00" 

The textFieldDidBeginEditing method selects all editable areas, another valid / useful choice would be between 1234 and . .

Please note that we work with both UITextRange and NSRange . They are not interchangeable.

 extension MyViewController : UITextFieldDelegate { /// Set the "insertion caret" for the text field. func textFieldDidBeginEditing(_ textField: UITextField) { if let startPosition = textField.position(from: textField.beginningOfDocument, offset: 1), // forward over "$" let endPosition = textField.position(from: textField.endOfDocument, offset: -3) { // back 3 over ".00" let selection = textField.textRange(from: startPosition, to: endPosition) //let selection = textField.textRange(from: endPosition, to: endPosition) textField.selectedTextRange = selection } } /// Only allow edits that are in the editable range and only adding digits or deleting anything (this allows cut/paste as well) func textField(_ textField:UITextField, shouldChangeCharactersIn range:NSRange, replacementString string:String) -> Bool { let minimalString = "$.00" // String always starts in uneditable "$", and ends in an uneditable ".00" assert(textField.text?.count ?? 0 >= minimalString.count) guard let text = textField.text else { return false } if range.upperBound > text.count - 3 || range.lowerBound == 0 { return false } // range only in editable area if string.components(separatedBy: CharacterSet.decimalDigits.inverted).joined().count != string.count { return false } // new string only has digits return true } } 
+1
source

As far as I know, there is no easy way to do this.

I would say that the easiest way to do this is to add a suffix after the user enters its text. Remember to add something to your user interface to let the user know that something has been added.

0
source

You can have a suffix like UILabel right next to the end of a UITextField, or you can add your delegate to add a suffix after editing is complete, for example:

 @interface MyViewController : UIViewController <UITextFieldDelegate> {...} ... [textField setDelegate:self]; ... - (void)textFieldDidEndEditing:(UITextField *)textField { NSString *textWithSuffix = [[NSString alloc] initWithFormat:@"%@%@", [textField text], @"SUFFIX"]; [textField setText:textWithSuffix]; [textWithSuffix release]; } 
0
source

All Articles