在我的项目中,我想打开iOS内置词典来查找单词含义,甚至更好,直接在我的应用程序中获取单词的含义. 目前我发现如何使用UITextChecker检查字符串拼写是否正确 func wordIsReal(word: String) -
目前我发现如何使用UITextChecker检查字符串拼写是否正确
func wordIsReal(word: String) -> Bool { let checker = UITextChecker() let range = NSMakeRange(0, count(word)) let misspelledRange = checker.rangeOfMisspelledWordInString(word, range: range, startingAt: 0, wrap: false, language: "en") return misspelledRange.location == NSNotFound }我找到了Object-C的解决方案:
if ([UIReferenceLibraryViewController dictionaryHasDefinitionForTerm:@"word"]) { UIReferenceLibraryViewController* ref = [[UIReferenceLibraryViewController alloc] initWithTerm:@"word"]; [currentViewController presentViewController:ref animated:YES completion:nil]; }
我为Swift 3编辑了它:
let word = "home" if UIReferenceLibraryViewController.dictionaryHasDefinitionForTerm(word) { let ref: UIReferenceLibraryViewController = UIReferenceLibraryViewController(term: word) self.presentViewController(ref, animated: true, completion: nil) }
和Swift 4一样:
let word = "home" if UIReferenceLibraryViewController.dictionaryHasDefinition(forTerm: word) { let ref: UIReferenceLibraryViewController = UIReferenceLibraryViewController(term: word) self.present(ref, animated: true, completion: nil) }
如果单词在设备中保存的词典中有定义,则此解决方案允许打开内置字典