2

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?

Aaron Hayman
  • 8,492
  • 2
  • 36
  • 63
  • Did you have any difficulty getting the auto complete to appear at all? I have a working text editor, but I can't get the auto complete to appear at all. – BarrettJ Oct 09 '12 at 21:05
  • Off the top of my head I can think of several reasons why it might not appear. 1)You're providing an incorrect CGRect for `firstRectForRange`. 2)One of the many methods that calculate position or offset are wrong. 3)You are returning a value for `markedTextRange` when there should be none. Debugging UITextInput can be a big pain. I've found it helpful to NSLog all protocol methods along with their input values and return values to find the bug. You might find this useful: `NSLog(@"%s", __PRETTY_FUNCTION__);`. This log the class/method that it's placed in. You'll have to log values manually. – Aaron Hayman Oct 10 '12 at 00:22
  • The simulator might not prompt auto-correct when you type on the computer's physical keyboard. Try clicking the keyboard buttons on the simulator's screen. – Walt Sellers Dec 08 '12 at 03:10

1 Answers1

3

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

- (NSDictionary *)textStylingAtPosition:(UITextPosition *)position inDirection:(UITextStorageDirection)direction;

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:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch;

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:

- (UITextAutocorrectionType)autocorrectionType {
    return UITextAutocorrectionTypeDefault;
}
Mat K Hall
  • 46
  • 4
  • I am conforming to the UITextInputTraits. I've confirmed that the properties are properly synthesized by changing appropriate initialization values (like keyboard appearance and turning auto correct on/off) and observing the correct changes in keyboard appearance/behavior. So as far as I can tell everything is set up correctly. – Aaron Hayman Jun 15 '12 at 11:58
  • You're correct in that my view was still receiving the touch event and changed the selected range, which prompted the auto-correct to submit instead of cancel. However, all I had to do was implement `- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{ return (touch.view == self); }`. The autocorrect now cancels as it should. Thanks! – Aaron Hayman Aug 06 '12 at 15:49