在我的项目中,我想打开 iOS 内置词典 来查找词义,或者更好的是,直接在我的应用中获取词义。
目前我发现了如何使用 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
}
最佳答案
我找到了Objective-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)
}
如果单词在设备中保存的词典中有定义,此解决方案允许打开内置词典
关于ios - Swift - 打开内置的 iOS 字典来查找词义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32603825/