spigen
spigen

Reputation: 161

Turn on Shift key when spacebar is pressed in UITextField

How do I programmatically turn on the shift key (so that the next character is uppercase) on the keyboard when the user presses the spacebar for a UITextField?

In addition, I do not want any spaces in the text field such that the user is able to type an entry like this: "ThisIsATest"

I currently use the following to detect if the spacebar is pressed, and return false to avoid spaces, but I dont know how to toggle the shift key:

// check if string in UITextField is a space
if (string == " "){ 
// do not return a space
return false
}

Upvotes: 2

Views: 2008

Answers (3)

NKorotkov
NKorotkov

Reputation: 3681

UPDATED ANSWER

This is tested and does print "ThisIsATest" when you press space bar after each word. I'm not that good with Swift yet, so this snippet is in Obj-C. It's pretty straightforward. Be sure to set your textField's delegate to ViewController either in code or in IB.

#import "ViewController.h"

@interface ViewController () <UITextFieldDelegate>

@property (nonatomic, assign) BOOL shouldCapitalizeNextChar;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.shouldCapitalizeNextChar = YES;

}


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


    if ([string isEqualToString:@" "]) {

        self.shouldCapitalizeNextChar = YES;

        return NO;
    }

    if (self.shouldCapitalizeNextChar) {

        NSString *capitalizedChar = [string capitalizedString];

        textField.text = [textField.text stringByAppendingString:capitalizedChar];

        self.shouldCapitalizeNextChar = NO;

        return NO;
    }

    return YES;
}


@end

As before, this won't work with copied and pasted text. But it should help you to get the idea.

ORIGINAL ANSWER

A UITextField knows how to autocapitalize each word, if that's what you want in your app just use:

Obj-c

textField.autocapitalizationType = UITextAutocapitalizationTypeWords;

Swift

textField.autocapitalizationType = UITextAutocapitalizationType.Words

Be aware that this is just a suggestion to the user, he/she can override it by pressing shift. It won't work if Paste option is used either.

Upvotes: 0

Nirav Gadhiya
Nirav Gadhiya

Reputation: 6342

You need to set autoCapitalizationType of a textField. For your requirement you need to set Words

From The Docs :

Discussion This property determines at what times the Shift key is automatically pressed, thereby making the typed character a capital letter. The default value for this property is UITextAutocapitalizationTypeSentences.

SWIFT

enum UITextAutocapitalizationType : Int {
    case None
    case Words
    case Sentences
    case AllCharacters
}

Constants (For All : Available in iOS 2.0 and later.)

None : Do not capitalize any text automatically.

Words : Capitalize the first letter of each word automatically.

Sentences : Capitalize the first letter of each sentence automatically.

AllCharacters : Capitalize all characters automatically.

OBJECTIVE C

typedef enum : NSInteger {
   UITextAutocapitalizationTypeNone,
   UITextAutocapitalizationTypeWords,
   UITextAutocapitalizationTypeSentences,
   UITextAutocapitalizationTypeAllCharacters,
} UITextAutocapitalizationType;

Constants (For All : Available in iOS 2.0 and later.)

UITextAutocapitalizationTypeNone : Do not capitalize any text automatically.

UITextAutocapitalizationTypeWords : Capitalize the first letter of each word automatically.

UITextAutocapitalizationTypeSentences : Capitalize the first letter of each sentence automatically.

UITextAutocapitalizationTypeAllCharacters : Capitalize all characters automatically.

Just like:

SWIFT

textField.autocapitalizationType = UITextAutocapitalizationType.Words

OBJECTIVE C

textField.autocapitalizationType = UITextAutocapitalizationTypeWords;

Enjoy....

Upvotes: 3

Ankush
Ankush

Reputation: 142

I think the problem is

string == @" " is wrong.

Equality for strings is done using:

[string isEqualToString:@" "]

I hope it will help:)

Upvotes: 0

Related Questions