iPhone restrict user from entering space in textfield

前端 未结 6 1974
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-05 08:40

i want to restrict user from entering space in a UITextField. for this i m using this code

- (BOOL)textField:(UITextField *)textField shouldChangeCharacters         


        
6条回答
  •  借酒劲吻你
    2021-01-05 09:33

    Try this (set this in your - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string):

    NSArray *escapeChars = [NSArray arrayWithObjects:@" ", nil];
    
    NSArray *replaceChars = [NSArray arrayWithObjects:@"",nil];
    int len = [escapeChars count];
    NSMutableString *temp = [[textField text] mutableCopy];
    for(int i = 0; i < len; i++) {
        [temp replaceOccurrencesOfString: [escapeChars objectAtIndex:i] withString:[replaceChars objectAtIndex:i] options:NSLiteralSearch range:NSMakeRange(0, [temp length])];
    }
    [textField setText:temp];
    return TRUE;
    

提交回复
热议问题