天天看點

UIReferenceLibraryViewController的使用(調用系統詞典)

在程式中使用蘋果的詞典來查詢詞彙,即一些軟體的“詞典"功能

UIReferenceLibraryViewController和MFMessageComposeViewController很相似,提供了最小化配置的系統層viewController,可以直接被present顯示。

可用需要查找term來進行初始化:

    UIReferenceLibraryViewController *referenceLibraryViewController =

    [[UIReferenceLibraryViewController alloc] initWithTerm:@"apple"];

    [viewController presentViewController:referenceLibraryViewController  animated:YES  completion:nil];

這種行為和使用者點選UITextView中高亮詞彙彈出的"定義"的UIMenuItem的效果差不多。

UIReferenceLibraryViewController也提供了一個類方法dictionaryHasDefinitionForTerm:,開發者可以在dictionary view controller出現之前調用這個方法,就可以在不必需的時候不顯示那個viewController了。

[UIReferenceLibraryViewController dictionaryHasDefinitionForTerm:@"apple"];

在這兩種情況下,UIReferenceLibraryViewController會以非常好的形式去格式化搜尋結果,是以并不需要開發者手動去掉空格或者調整大小寫來優化搜尋。