11

I have designed a custom keyboard extension and I am able to enter text properly. However I can't seem to be able to figure out when to enable a capitalized keyboard and when lowercase. This is mostly because the following functions don't correctly return the already entered before/after text. Sometimes they will return nil, sometimes only a last few characters etc. Many times these methods are not even called at all.

- (void)textWillChange:(id<UITextInput>)textInput 

- (void)textDidChange:(id<UITextInput>)textInput

I have figured out the following scenarios when I need to have enable my uppercase keyboard:

  1. Text length =0 or nil
  2. When a period is entered, I need to add a space and uppercase keyboard
  3. When the cursor is placed at a location where the character before cursor is either a period or a space and before that is a period.
  4. When user selected the entire text "Select All"
  5. When user selected the entire text and deleted all
  6. When user backspaces and the new character before cursor is either a period or a space and before that is a period.
  7. When user pastes text and the character before cursor is either a period or a space and before that is a period.

Also if the above is possible, is it possible to also get the last entered "entire" word which I can use for dictionary searches like for predictive typing?

I have read the apple document where it states that users expect auto-capitalization, so I am sure this should be possible

https://developer.apple.com/library/ios/documentation/General/Conceptual/ExtensibilityPG/Keyboard.html

1 Answers1

2

E.G. textField being named of your UITextField instance. Put this after initializing.

textField.autocaptializationType = UITextAutocapitalizationTypeSentences

That should give you capital letters in all of the instances you mentioned.

Ok to make your custom keyboard adopt UITextInputTraits protocol, do the following.

@interface MyCustomKeyboard : UITextInput <UITextInputTraits> //Now adopts the UITextInputTraits protocol.

 - (instancetype)init {
     self = super.init;

     if(self) {
         //Here you can set the properties that come with the protocol
         self.autocapitilizationType = UIAutocapitalizationTypeSentences;

      }
}
NSGangster
  • 2,397
  • 12
  • 22
  • How do I get the UIInputText instance? I don't see that anywhere :( – sudoExclaimationExclaimation Jan 07 '16 at 18:14
  • oh i think you are onto something! Let me try reading more about the `UITextInput` which the text did change method returns and see if there is a way to find the text. will get back to you! – sudoExclaimationExclaimation Jan 08 '16 at 03:29
  • The biggest problem with Xcode in making keyboard extension apps is that slog and break points don't work with the device ugh. Will get back to you after testing on simulator – sudoExclaimationExclaimation Jan 08 '16 at 03:35
  • I have been trying to get something going with the UITextInput but that's not an object. It's a protocol. And looking at the documentation, I don't see any way of enabling UITextAutocapitalizationTypeSentences on it. DO you have any other ideas? – sudoExclaimationExclaimation Jan 10 '16 at 19:27
  • So what? If you have you UITextInput variable input, you can check if it conforms to the protocol UITextInputTraits, and if it does, cast it: id inputTraits = input; inputTraints.autocapitalizationType - is a value you need. – Orest Savchak Jan 11 '16 at 18:29
  • I have tried: `- (void)textWillChange:(id)textInput { if ([textInput conformsToProtocol:@protocol(UITextInputTraits) ]) { UITextInputTraits *inputTraits = (UITextInputTraits*)textInput; inputTraits.autocaptializationType = UITextAutocapitalizationTypeSentences; } }` but this doesn't work and throws error – sudoExclaimationExclaimation Jan 12 '16 at 02:54
  • Your `if ([conformsToProtocol...])` conditional won't evaluate to true unless you actually set your class to conform to the protocol. I've updated my answer. You want to set up UITextInputTraits in init unless its a property that will change based on state of the keyboard. Autocapitalization should stay the same. – NSGangster Jan 12 '16 at 15:59
  • https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextInputTraits_Protocol/ If you read the first paragraph of this you will see why I usually just set the property on my instance of UITextField or UITextView. But I should have realized you may have needed to create a custom keyboard for your own reasons. – NSGangster Jan 12 '16 at 16:02