Search code examples
objective-cioscore-textuikeyboarduitextinput

Custom Text Field – Mirrored Autocorrect


I'm implementing a custom UITextView (based on EGOTextView) using CoreText and conforming to the UITextInput protocol. I have almost everything working fine (phew!), except for one annoying thing. Autocorrect suggestion text is mirrored vertically and its highlight is slightly shifted to the right. Here's what it looks like:

Mirrored autocorrection

In the text field I typed "helo", which it autocorrects to "help." As you can see, the autocorrect text, but not its background, is vertically mirrored. Also, it's horiztontally offset to the right by ~7pt.

To address the second problem (the horizontal offset), I have verified that the firstRectForRange: method returns the correct CGRect. I've done this in two ways. The first was to visually check that when I display a UIMenuController it shows up in the right spot (it does). The second is to draw an outline around the CGRect returned by firstRectForRange:. Here's the same text with the CGRect outlined.

Outlined CGRect

As you can see, the correct area is outlined, but the autocorrect is marked/highlighted incorrectly.

I'm happy to share any code, but it's a huge class and I'm pretty stumped now. Any pointers would be massively appreciated!

Edit: The source code is available on the Experimental branch of this repository: github.com/cbrauchli/EGOTextView.


Solution

  • I aslo faced same same problem while trying your code and I found that there is no need to implement

    - (UIView *)textInputView {
        return _textContentView;
    }
    

    See Apple documentation (This is typically an instance of the UITextInput-adopting class.) for more help on it. And if you want to keep implementation of this method, just return self from it, as here self (EGOTextView) is a instance of the UITextInput-adopting class.

    I tried this at my end and works fine.