Can I set the insertion point on a UITextField
I have a UITextField
that I want to 开发者_如何学Pythonpresent to the user with a pre-filled suffix. Obviously, I'd like to have the insertion point set to the beginning of the pre-filled text.
It doesn't seem obvious to me how to do this (e.g., there's no insertionPoint
property), but is there a tricky way to get this done?
Override drawTextInRect: to always draw your suffix, even though it is not contained in the UITextFields text (use a temp string that takes the textfield.text, appends your suffix, and draws it). Once the user is done editing, then append your suffix to the string (or use them separately if it helps).
Everybody loves a 9 year old question.
UITextField conforms to UITextInput. So all the methods you're looking for in the class documentation are nowhere to be found.
In my example, A string of "$1234.00"
was to be displayed, but only a 1234
range of the string could be edited.
textField.delegate = self
textField.text = "$\(myIntValue).00"
The textFieldDidBeginEditing
method selects all of the editable area, another valid/useful selection would be between the 1234
and the .
.
Note that we are working with both UITextRange
an NSRange
. They aren't exactly 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
}
}
As far as I know, there's no simple way to do this.
I'd say that the simplest way to do this is to add the suffix after the user entered his text. Don't forget to add something in your UI to tell the user there will be something added.
You could have the suffix as a UILabel right next to the end of the UITextField, or you could have your delegate add the suffix after editing is done, like:
@interface MyViewController : UIViewController <UITextFieldDelegate> {...}
...
[textField setDelegate:self];
...
- (void)textFieldDidEndEditing:(UITextField *)textField {
NSString *textWithSuffix = [[NSString alloc] initWithFormat:@"%@%@", [textField text], @"SUFFIX"];
[textField setText:textWithSuffix];
[textWithSuffix release];
}
精彩评论