UITextInput: selectedTextRange vs. markedTextRange?
Asked Answered
B

1

9

Ok, so I know Apple's UITextInput protocol requires the two UITextRange properties selectedTextRange and markedTextRange and the documentation says that selectedTextRange is a subrange of markedTextRange which is an uncomfirmed text range by the user yatta yatta. That still doesn't make some things clear to me regarding how I ought to implement the two text ranges differently. Could someone visually explain to me the difference between selectedTextRange and markedTextRange? I know that when the length of selectedTextRange is 0 it indicates a blinking caret at selectedTextRange's location. But what the heck is "marked text"?? I've only seen the following for text views in iOS:

example of <code>selectedTextRange</code>

Which I assume represents the current selectedTextRange. What does markedTextRange look like? Or is it basically the exact same thing? I'm so confused :( Thanks in advance for any help! The documentation has proven itself useless in my understanding of how to implement the UITextInput protocol.

EDIT

Does implementing markedTextRange have anything to do with the fact that some text in a view could be "markable" but "readonly" and selectedTextRange indicates the subrange in the "marked text", markedTextRange, that is readwrite?

Bioastronautics answered 8/7, 2014 at 13:7 Comment(0)
F
14

From Apple documentation for UITextInput:

Marked text, which is part of multistage text input, represents provisionally inserted text that the user has yet to confirm. It is styled in a distinctive way. The range of marked text always contains within it a range of selected text, which might be a range of characters or the caret.

Hence markedTextRange gets useful with languages that requires multistage input, e.g. Japanese. In simple words: what user types is yet to be confirmed before it can be added to the value of the text input control is were markedTextRange gets into the game. GIF bellow demonstrates markedTextRange in action:

enter image description here

Notice slight sapphire background behind the unconfirmed hieroglyphs. Once text gets confirmed either by hitting enter/return, selecting option from suggestions or finger tap on text area after the marked text gets added to the input control value and background gets removed.

Notes:

  • markedTextRange has nothing to do with read-only text
  • I was not able to achieve multiple symbols selection within markedTextRange
Fingertip answered 8/7, 2014 at 22:20 Comment(4)
Can you advise how to properly limit max text input length? i.e. if i limit input to 3 chars and try to input "ででで" which is achieved when i press keyboard buttons "dedede"(each "de" transforms into "で") then i am not able to input the last character, because my validation function considers that to be 4 characters. Here is my validation function: -(BOOL)textField:(UITextField*)tf shouldChangeCharactersInRange:(NSRange)r replacementString:(NSString*)s {return [tf.text stringByReplacingCharactersInRange:r withString:s].length <= 3;} This can be reproduced on Japanese - Romaji keyboardFormal
i mean, i could still use a completion popup sticked to top of a keyboard, but that seems like a workaround.. or am i overlooking this..?Formal
@Formal consider always returning true in case textField.markedTextRange != nilGaynor
with regards to e.g. Japanese -- the mutlistage input is also important to support for any language which has autocorrect behavior going on. Try to type trst in a message field for example, probably the text will "marked" and replaced on space barBurgenland

© 2022 - 2024 — McMap. All rights reserved.