目标-C中的UILexicon [英] UILexicon in Objective-C

查看:300
本文介绍了目标-C中的UILexicon的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Objective-C中使用UILexicon?我发现Apple提供的文档非常无用。

How do you use UILexicon in Objective-C? I find the documentation Apple provides is extremely unhelpful.

它做了什么?它会返回字典或正确的单词拼写吗?或者我提供像hellllo这样的单词并将其与正确的拼写Hello匹配并将其作为字符串返回?

What does it do? Does it return a dictionary or proper spellings of words? Or do I provide a word like "hellllo" and it matches it with the proper spelling "Hello" and returns that as a string?

任何帮助都将不胜感激。

Any help would be appreciated.

requestSupplementaryLexiconWithCompletion:

这是我的错误报告,但显然我会有错误,因为我完全猜测如何使用该函数,不知道块语句内部是什么(因为文档(当时)不说!(Beta 4 docs))哈哈哈!

Here's my error report, but obviously I'll have errors because I'm completely guessing how to use the function, no clue what goes inside the block statement (because the docs (at the time) don't say! (Beta 4 docs)) Hahahah!

推荐答案

我从来没有使用过这个功能,但是对UILexicon的快速网络搜索使我获得了Apple的文档;从那里读取和关注链接非常快。

I've never used this feature, but a quick web search for "UILexicon" landed me in Apple's documentation; reading and following links from there filled in the picture pretty quick.

App Extension Programming Guide 可快速解释词典的用途:

App Extension Programming Guide has a quick explanation of what lexicons are for:


每个自定义键盘(独立于其 RequestsOpenAccess 键的值)通过 UILexicon 类。当用户输入文本时,请使用此类以及您自己设计的词典来提供建议和自动更正。

Every custom keyboard (independent of the value of its RequestsOpenAccess key) has access to a basic autocorrection lexicon through the UILexicon class. Make use of this class, along with a lexicon of your own design, to provide suggestions and autocorrections as users are entering text.

点击 <$ c该页面上的$ c> UILexicon 链接将我带到该类的参考文档,这解释了它是Apple提供的术语对的只读列表。其每个条目都是 UILexiconEntry 对象 - 该类的文档说它提供了 userInput (用户键入的内容,例如ipad)和 documentText (替代它的是什么,例如iPad)。由于这些类是只读的,因此它们可能不是您提供自己的自动更正对的方式 - 正如文档中所述,它们用于补充您实现的任何自动更正系统。

Clicking the UILexicon link on that page took me to the reference doc for that class, which explains that it's a read-only list of Apple-provided term pairs. Each of its entries is a UILexiconEntry object -- the docs for that class say it provides a userInput (what the user typed, e.g. "ipad") and a documentText (what to substitute for it, e.g. "iPad"). Since those classes are read-only, it follows that they're probably not a way for you to provide your own autocorrection pairs -- as stated in the docs, they're for supplementing whatever autocorrection system you implement.

此时,我甚至不需要查看 requestSupplementaryLexiconWithCompletion: 很好地了解如何使用它:只是声明告诉我:

At this point, I don't even have to look at the doc for requestSupplementaryLexiconWithCompletion: to get a good idea how to use it: just the declaration tells me:


  • 这是 UIInputViewController ,我必须创建自定义键盘的类。在该子类的某个地方,我应该在 self 上调用它。

  • 它的返回类型是 void ,所以我无法通过将 requestSupplementaryLexiconWithCompletion 的调用结果分配给变量来获得词典。

  • 它调用我提供的块,向我传递一个 UILexicon 对象作为该块的参数。

  • 它有类似请求的字样并且它中的completionHander,所以它可能会做一些需要一段时间的异步,并在完成时调用该块。

  • It's a method on UIInputViewController, the class I'd have to subclass to create a custom keyboard. Somewhere in that subclass I should probably call it on self.
  • Its return type is void, so I can't get a lexicon by assigning the result of a requestSupplementaryLexiconWithCompletion call to to a variable.
  • It calls the block I provide, passing me a UILexicon object as a parameter to that block.
  • It's got words like "request" and "completionHander" in it, so it'll probably do something asynchronous that takes awhile, and call that block when it's done.

所以,我猜测如果我正在编写一个自定义键盘,我会尽早调用此方法(也许在 viewDidLoad 中)并隐藏 UILexicon 它提供了以后我可以在用户输入时引用它。这样的事情:

So, I'm guessing that if I were writing a custom keyboard, I'd call this method early on (in viewDidLoad, perhaps) and stash the UILexicon it provides so I can refer to it later when the user is typing. Something like this:

@property UILexicon *lexicon;

- (void)viewDidLoad {
    [super viewDidLoad];
    [self requestSupplementaryLexiconWithCompletion:^(UILexicon *lexicon){
        self.lexicon = lexicon;
    }];
}

因为不清楚多长时间 requestSupplementaryLexiconWithCompletion 将完成,我正在使用的任何地方 self.lexicon 我应该检查它是否 nil

Because it's unclear how long requestSupplementaryLexiconWithCompletion will take to complete, any place where I'm using self.lexicon I should check to see if it's nil.

返回 App Extension编程指南,它列出了自动更正和建议 在iOS用户期望的键盘功能下,就在说:

Back in the App Extension Programming Guide, it lists "Autocorrection and suggestion" under "Keyboard Features That iOS Users Expect", right before saying:


您可以决定是否实现此类功能;对于刚刚列出的任何功能,没有专门的API

You can decide whether or not to implement such features; there is no dedicated API for any of the features just listed

所以听起来像自动修正是你必须自己做的事情,你的自己的用户界面是 提供的视图的一部分UIInputViewController 子类。 API编程指南中的自定义键盘快速入门部分似乎暗示了你如何做到这一点:使用 documentContextBeforeInput 查看用户最近输入的内容,< a href =https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIKeyInput_Protocol/index.html#//apple_ref/occ/intfm/UIKeyInput/deleteBackward> deleteBackward 摆脱它, insertText: 要插入修正。

So it sounds like autocorrection is something you have to do yourself, with your own UI that's part of the view presented by your UIInputViewController subclass. The API Quick Start for Custom Keyboards section in the programming guide seems to hint at how you'd do that: use documentContextBeforeInput to see what the user has recently typed, deleteBackward to get rid of it, and insertText: to insert a correction.

这篇关于目标-C中的UILexicon的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆