So I created a custom text view using core graphics and have it conformed to the UITextInput and UITextInputTraits protocols. Everything works fine except for one weird/annoying behavior. The keyboard correctly displays auto correct suggestions, but when the use taps on a suggestion labelled with an 'X', it doesn't dismiss the suggestion but instead inserts the suggestion. I've checked, and in all other programs tapping on the a suggestion with an 'X' dismisses the suggestion. How do I fix this?
In my custom text view I have the following iVars:
//UITextInputTraits
UITextAutocapitalizationType _uiAutoCap;
UITextAutocorrectionType _uiAutoCorrect;
UITextSpellCheckingType _uiSpellCheck;
UIKeyboardType _uiKeyboard;
UIKeyboardAppearance _uiKeyboardAppearance;
UIReturnKeyType _uiReturnType;
BOOL _uiEnableAutoReturn;
BOOL _uiSecureText;
Which are synthesized to the appropriate TextInputTraits properties:
@synthesize autocapitalizationType=_uiAutoCap, autocorrectionType=_uiAutoCorrect, spellCheckingType=_uiSpellCheck, keyboardType=_uiKeyboard, keyboardAppearance=_uiKeyboardAppearance, returnKeyType=_uiReturnType, inputDelegate=_uiTextDelegate, enablesReturnKeyAutomatically=_uiEnableAutoReturn, secureTextEntry=_uiSecureText;
And they're initialized with the following default values:
_uiAutoCorrect = UITextAutocorrectionTypeDefault;
_uiSpellCheck = UITextSpellCheckingTypeDefault;
_uiKeyboardAppearance = UIKeyboardAppearanceDefault;
_uiAutoCap = UITextAutocapitalizationTypeNone;
_uiReturnType = UIReturnKeyDefault;
_uiEnableAutoReturn = NO;
_uiSecureText = NO;
_uiKeyboard = UIKeyboardTypeDefault;
Any ideas?
Edit: Possible answer
When you tap to close the suggestion, your tap is likely intercepted by your view first which presumably changes the selected range of the text (which causes UITextInput to accept the suggestion). Not the best solution, but UITextInput calls
when it wants to make a suggestion, so, you could have an ivar (BOOL) that stores whether or not there is a suggestion (make its value NO whenever a UIKeyInput method is called and YES when the textStyling method is called). Then, modify your gesture recognizer so that it will not change the selection if the aforementioned ivar is YES and the tap is in the rect of the suggestion box (You could get this rect by doubling the height of the rect returned from - (CGRect)firstRectForRange:(UITextRange *)range;). Hope that works.
Edit: you should just be able to implement the UIGestureRecognizerDelegate method:
So that it only receives a touch if touch.view == (yourTextView)
I am having the same issue, and don't yet have a solution; however, I do believe that you should conform to UITextInputTraits by creating functions that return the value you would like for the property. Example: for the trait UITextAutoCorrectionType to have a value of UITextAutocorrectionTypeDefault, you should provide an accessor method: