2

Currently, I am setting autocapitalization in a button target like this

//This method is fired when keyboard is still active
- (IBAction)changeAutoCapitalization:(id)sender
{   
      self.textView.autocapitalizationType = UITextAutocapitalizationTypeWords;
}

But this won't make keyboard to capital.

Nikhil Manapure
  • 3,748
  • 2
  • 30
  • 55

4 Answers4

5

You have to call [self.textView reloadInputViews] after modifying the UITextAutocapitalizationType in order to change keyboard immediately.

Duyen-Hoa
  • 15,384
  • 5
  • 35
  • 44
1

If you find that you're doing a lot of text-related customizations, it might be helpful to subclass UITextView, to keep the functionality encapsulated.

Note: If testing on the simulator, make sure you are using the Simulator's keyboard, by going to the menu: I/O -> Keyboard -> Toggle Software Keyboard

If you are adding text automatically, for instance in response to a button tap, and the shift key turns off, then you can use this pattern:

autocapitalizationType = .allCharacters //Activate shift key
autocorrectionType = .no //Don't show all-caps auto suggest
reloadInputViews() //Apply changes

Then, to restore to sentences capitalization, for instance when a line is cleared:

if (autocapitalizationType = .allcharacters) {
    autocapitalizationType = .sentences //Activate sentence capitalization
    autocorrectionType = .yes //Show auto suggest
    reloadInputViews() //Apply changes
}

Other capitalization options:

.none
.words

You may also need to do this when any non-enter key is pressed. To do so, override insertText from UIKeyInput, and check the entered text string:

if (text != "\n") {
    if (autocapitalizationType == .allCharacters) {
        autocapitalizationType = .sentences //Activate sentence capitalization
        autocorrectionType = .yes //Show auto suggest
        reloadInputViews() //Apply changes
    }

    //Respond to any character sequences
    //...
}
Sheamus
  • 6,506
  • 3
  • 35
  • 61
0

Try to add this after initialization:

self.textView.autocapitalizationType = UITextAutocapitalizationTypeWords;

OR

It's very likely that the "Auto-Capitalization" option is turned off on that device. You can find that option in Settings > General > Keyboard.

Yatish Agrawal
  • 452
  • 5
  • 15
0

In My case I was using 'Keyboard Type' as Name pad in storyboard.

Changing 'Keyboard Type' back to 'Default' resolved my issue.

enter image description here

Also added this code in viewDidLoad

textfieldCardName.autocapitalizationType = .allCharacters
Alamgir
  • 21
  • 5