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 } }
source share