How to programmatically turn on Shift key when spacebar is pressed in UITextField?

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 }

  • Answer:

    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.

spigen at Stack Overflow Visit the source

Was this solution helpful to you?

Other answers

You need to set autoCapitalizationType of a textField. For your requirement you need to set Words From The https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextInputTraits_Protocol/#//apple_ref/occ/intfp/UITextInputTraits/autocapitalizationType : 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....

N J Gadhiya

I think the problem is string == @" " is wrong. Equality for strings is done using: [string isEqualToString:@" "] I hope it will help:)

Ankush

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.