Abha
Abha

Reputation: 1082

Remove WhiteSpace from start of uitextfield

I want to remove white space from start of word. i.e my first character should not be space and having only one space in between two words .I am trying lots of things but not able to remove space from starting.

My code is:

controller.h

int whitespaceCount;

controller.m

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSCharacterSet *whitespace = [NSCharacterSet whitespaceAndNewlineCharacterSet];
    if ([string rangeOfCharacterFromSet:whitespace].location != NSNotFound)
    {
        whitespaceCount++;
        if (whitespaceCount > 1)
        {
            return NO;
        }
    }
    else
    {
        whitespaceCount = 0;
        return YES;
    }
}

from above code one space between two words are works perfectly. Please suggest me how i remove starting space. Thanks in advance.

Upvotes: 7

Views: 16015

Answers (5)

Nitin Gohel
Nitin Gohel

Reputation: 49730

you can do with method :-

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

    if (range.location == 0 && [string isEqualToString:@" "]) {
        return NO;
    }
    return YES;
} 

Or For latest Swift, you can use following:

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

    if range.location == 0 && (string == " ") {
        return false
    }
    return true
}

using this you cannot able To enter whitescpace at starting.

and you can do also like that:-

 NSCharacterSet *whitespace = [NSCharacterSet whitespaceAndNewlineCharacterSet];

if you want to remove it from your string , you need to work on following

NEW STRING> = [<STRING_CONTAINS WHITESPACE> stringByTrimmingCharactersInSet:whitespace];
 NSLog("String without white space = %@",NEW STRING);

Or if you want to remove whitespace entirely from the textfield use following:

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

    if (string == " ") {
        return false
    }
    return true
}

And to make this method work, make sure to mark your textfield.delegate = self

Upvotes: 24

Akhil
Akhil

Reputation: 373

Better to prevent user from entering space as first character in you UITextField. Here we will prevent user from entering space as first character. User will be allowed to enter space in between the string as well as at the end of the string.

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
//Will prevent user from entering space as first character
let enteredCharString = "\(textField.text ?? "")\(string )"
if enteredCharString .trim().count == 0 {
  return false
}
}

Upvotes: 0

ajithmn89
ajithmn89

Reputation: 249

trim the text inside a your textfield when you going to use it by

myTextfiled.text = [myTextfiled.text stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]];

this will trim the whitespace from the begining as well as at the end

Upvotes: 7

spaleja
spaleja

Reputation: 1435

Use this method to remove white spaces:

+(NSString *)trim:(NSString *) strInput{
    return [strInput stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}

Upvotes: 0

Ossir
Ossir

Reputation: 3145

Method below perfectly removes spaces before and after text: string = [string stringByTrimmingCharactersInSet:whitespace];

Upvotes: 0

Related Questions