views:

238

answers:

3

I have a UITextField that I want to present 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 (there's no insertionPoint property), but is there a tricky way to get this done?

A: 

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.

gcamp
Its a suffix he's wanting, not a prefix (not that it makes much difference).
Mk12
Changed, thanks Mk12!
gcamp
A: 

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];
}
Mk12
+3  A: 

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).

mahboudz
Though it would seem I can't do exactly what I'd like to do, I think this is the closest 'simulation' of what I want. Thanks.
Joshua J. McKinnon