Add constant character to UITextField

Is there a way to constantly add a letter to UITextField where the user cannot delete it? I want to add one character, and the user cannot delete it, but after that they can add letters.

Greetings

ps This is for iOS

+4
source share
3 answers

A UITextFieldhas a delegate method that should change characters in a range, this method basically asks the question: should the next character be added or removed? and from this you can dictate what you want. Here is a sample code.

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

    BOOL shouldChangeCharacters = ( (textField.text.length + string.length) > 1 ); 
    return shouldChangeCharacters;

}

, 1, , , ...

, , - , - , .

if (range.location == 0) {
    NSString* firstCharacter = [string substringToIndex:1];
    BOOL firstCharacterIsDesiredCharacter = [firstCharacter isEqualToString:@"#"];
    if ( !firstCharacterIsDesiredCharacter ) {
        NSString* symbolWithText = [NSString stringWithFormat:@"#%@",text];

        //  ******************** \\

        [textView setText:symbolWithText];
        return NO;

        // or we could do this

        string = symbolWithText;

        //  ******************** \\

    }

}

... . .. - , .

, , -, - 0 1. , 0, , . , . , UITextField #work, "work" "workwork", , - .

UITextField Reference

+5

   UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
   label.text = @"left";
   label.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.0];
   textField.leftViewMode = UITextFieldViewModeAlways;
   textField.leftView = label;
0

:

.h:

IBOutlet UITextFeild *textFeild;

.m:

- (BOOL)textFieldShouldReturn:(UITextField *)textFeild {

NSString *textFeildText = textFeild.text;
textFeild.text = [NSString stringWithFormat:@"string you want to always add %@",textFeildText];

return NO;

}

, , , , . , , , - :

String you want to add String you want to add Hello World!

, , , , ! , , , textFeildShouldReturn "return NO":

[textFeild resignFirstResponder];
0

All Articles