Search code examples
iosdelegatesuitextfielduialertcontroller

How Can I restrict the prefix textfield string in iOS?


How Can I restrict the prefix of textField string.

I want the effect that textfield have default prefix string(AAAA-).

(AAAA- after user add string)

And user can't delete these string, user can add the append the string to the textField.

How can I use below delegate method achieve the effect?

-(BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
 {

 }

I don't want custom the modal view to achieve these effect.

Have some textfield method can achieve?

thank you

--- detail explain question ---

I use the UIAlertAction add the textfield in AlertAction like below:

at the interface I declare

NSString *_prefixString;

in the viewdidload set the initvalue

_prefixString = @"AAAA-";

And I had set the UITextFieldDelegate in header file.

Then the alertviewcontroller set below:

    UIAlertController *changeDeviceNameDialog =[UIAlertController
                                             alertControllerWithTitle:NSLocalizedString(@"change_device_name",nil)  message:nil preferredStyle:UIAlertControllerStyleAlert];

         UIAlertAction *cancelChangeDevNameAc = [UIAlertAction actionWithTitle:NSLocalizedString(@"cancel",nil) style:UIAlertActionStyleDefault handler:nil];
         UIAlertAction *enterChangeDevNameAc = [UIAlertAction actionWithTitle:NSLocalizedString(@"enter",nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){

        UITextField *newDeviceNameTF = changeDeviceNameDialog.textFields[0];
      }];

    [changeDeviceNameDialog addAction:cancelChangeDevNameAc];
    [changeDeviceNameDialog addAction:enterChangeDevNameAc];
    [changeDeviceNameDialog addTextFieldWithConfigurationHandler:^(UITextField *textField){
        textField.delegate = self;
        textField.placeholder = NSLocalizedString(@"new_device_name", nil);
        textField.text = _prefixString;
        }];
       [self presentViewController:changeDeviceNameDialog animated:YES completion:nil];

// below refer the replay

     -(BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
     {
      if ([textField.text isEqualToString:_prefixString] && range.location == _prefixString.length - 1 && range.length == 1) {
              return NO;
         }
      return YES;
     }

I want the effect is when the alert pop, the textfield have the default text AAAA-,and the user can't clear the AAAA- text.

User just can enter other text append the AAAA-.

But the delegate like upper, I still can remove the prefix text.


Solution

  • Try this,

    Set your prefixString,

    self.prefixString = @"PREFIX";
    

    Then in delegate method,

    -(BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
    {
    
      if ([textField.text isEqualToString:_prefixString] && range.location == _prefixString.length - 1 && range.length == 1) {
    
          return NO;
      }
    
      return YES;
    }