6

根据文档

标记文本是多级文本输入的一部分,表示用户尚未确认的临时插入文本。它以独特的方式设计。标记文本范围内始终包含选定文本范围,可能是字符范围或插入符号。

管理键盘一章中,图 5-2 显示了标记的文本是什么:

在此处输入图像描述

给定第 4 和第 6 个图像,键盘上的顶部栏显示标记的文本,如“修”、“修改”、“修身”等。

我感到困惑的是 HOWTO :

  • 检测用户点击标记的文本。它是替换选定的文本,或者只是插入标记的文本。
  • 在栏上显示自定义文本。例如,[textView showMarkedText:@"hello" atIndex:0]

谢谢。

4

1 回答 1

4

Actually, you are kind of confused with the "marked text" and the "candidate text".

Take the Chinese-Handwriting keyboard for instance, the symbol "修" lies on the note is called "marked text", which has a distinctive style. And the "修", "修改", "修身", ... on the keyboard, you can call them "candidates"(The bar where they placed is named "candidate bar", if you inspect the view hierarchy of the keyboard).

There are some interfaces to operate on marked text. You could take a look at UITextInput protocol which UITextView and UITextField are confirmed to. -setMarkedText:selectedRange: and -unmarkText will do the tricky.

There is no public API to operate on the candidate bar and how to detect the tap on it is undocumented as well. But you can indirectly detect that by implementing -textView:shouldChangeTextInRange:replacementText: and -textViewDidChange: in UITextViewDelegate protocol if you are using UITextView (or the counterpart methods for UITextField if you are using it).

P.S: There is, at least, as I know, an exception for the Chinese keyboard, which is kind of like a bug. When you tap the candidate bar of a Chinese keyboard, the text you tapped is applied to your UITextView(or UITextField) without -textView:shouldChangeTextInRange:replacementText: triggered. But -textViewDidChange: will be invoked eventually.

Hope it could help.

于 2015-12-25T03:35:46.813 回答