Meaning of Offset in UITextInput

Hi,
I have doubts on the meaning of offset in various methods of UITextInput. As a convention, let call "utf" offset the value returned by utf16offset of String and "character" the offset corresponding to the visible Character. Thus, between the start and end of "👨‍👩‍👧‍👦", there are 1 character position and 11 utf positions.

For the method position(from position: UITextPosition, offset: Int) -> UITextPosition?, it seems that offset means characters: the text input will call this method with offset = -1 when deleting backward and we have to return the whole emoji range for example. Otherwise, we will return an invalid position "inside" the emoji and break it appart. The -1 seems to come from the text input system and I didn't find any call to other methods to generate it.

For the method func offset(from: UITextPosition, to toPosition: UITextPosition) -> Int, the documentation states "Return the number of UTF-16 characters between one text position and another text position." So that's utf offset and we must return 11 for the emoji. This seems confirmed by UIStringTextInputTokenizer which uses this method to mesure things and will return wrong results if one returns characters offsets.

This bothers me however because it really seems that both functions are reciprocal, and the distinction for the meaning of offset only comes from experiences at runtime.

Can someone confirm that offsets have indeed different meanings in both functions?

Thank you!

Replies

Working on the same thing. I assume this is in regards to a custom UITextInput implementation? Are you also using TextKit as backend? Are you using UITextInteraction?

Sorry, don't have an answer yet. Will post an update once I know more.
Yes, it's for a custom UITextInput implementation. I'm not using TextKit as my layout needs go beyond what's NSLayoutManager can do, but I store my model as a NSMutableAttributedString and my indexes are wrapping UTF16Offsets.