From a961ed3ab06434098ad9c851e910d01dbd4977af Mon Sep 17 00:00:00 2001 From: Kevin Jantzer Date: Wed, 13 Jan 2016 14:52:50 -0800 Subject: [PATCH] Added in-app dictionary #9 Define menu option is only available when one word is selected. --- Source/FolioReaderConfig.swift | 4 +++- Source/FolioReaderPage.swift | 16 +++++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/Source/FolioReaderConfig.swift b/Source/FolioReaderConfig.swift index 7ff261dac..fb072ce33 100755 --- a/Source/FolioReaderConfig.swift +++ b/Source/FolioReaderConfig.swift @@ -28,6 +28,7 @@ public class FolioReaderConfig: NSObject { public var localizedHighlightsTitle: String public var localizedHighlightsDateFormat: String public var localizedHighlightMenu: String + public var localizedDefineMenu: String public var localizedPlayMenu: String public var localizedPauseMenu: String public var localizedFontMenuNight: String @@ -63,9 +64,10 @@ public class FolioReaderConfig: NSObject { self.localizedHighlightsTitle = NSLocalizedString("Highlights", comment: "") self.localizedHighlightsDateFormat = "MMM dd, YYYY | HH:mm" - self.localizedHighlightMenu = NSLocalizedString("Highlight", comment: "") self.localizedPlayMenu = NSLocalizedString("Start Reading", comment: "") self.localizedPauseMenu = NSLocalizedString("Stop Reading", comment: "") + self.localizedHighlightMenu = NSLocalizedString("Highlight", comment: "") + self.localizedDefineMenu = NSLocalizedString("Define", comment: "") self.localizedFontMenuNight = NSLocalizedString("Night", comment: "") self.localizedFontMenuDay = NSLocalizedString("Day", comment: "") self.localizedReaderOnePageLeft = NSLocalizedString("1 page left", comment: "") diff --git a/Source/FolioReaderPage.swift b/Source/FolioReaderPage.swift index 1ab96ff75..09ca68dfb 100755 --- a/Source/FolioReaderPage.swift +++ b/Source/FolioReaderPage.swift @@ -334,7 +334,9 @@ extension UIWebView { // default menu } else { + if action == "highlight:" + || (action == "define:" && (js("getSelectedText()"))!.componentsSeparatedByString(" ").count == 1) || (action == "play:" && book.hasAudio() ) || (action == "share:" && readerConfig.allowSharing == true) || (action == "copy:" && readerConfig.allowSharing == true) { @@ -403,6 +405,17 @@ extension UIWebView { } } + func define(sender: UIMenuController?) { + let selectedText = js("getSelectedText()") + + setMenuVisible(false) + userInteractionEnabled = false + userInteractionEnabled = true + + let vc = UIReferenceLibraryViewController(term: selectedText! ) + vc.view.tintColor = readerConfig.toolBarBackgroundColor + FolioReader.sharedInstance.readerContainer.showViewController(vc, sender: nil) + } func play(sender: UIMenuController?) { @@ -468,6 +481,7 @@ extension UIWebView { let highlightItem = UIMenuItem(title: readerConfig.localizedHighlightMenu, action: "highlight:") let playAudioItem = UIMenuItem(title: readerConfig.localizedPlayMenu, action: "play:") + let defineItem = UIMenuItem(title: readerConfig.localizedDefineMenu, action: "define:") let colorsItem = UIMenuItem(title: "C", image: colors!, action: "colors:") let shareItem = UIMenuItem(title: "S", image: share!, action: "share:") let removeItem = UIMenuItem(title: "R", image: remove!, action: "remove:") @@ -477,7 +491,7 @@ extension UIWebView { let pinkItem = UIMenuItem(title: "P", image: pink!, action: "setPink:") let underlineItem = UIMenuItem(title: "U", image: underline!, action: "setUnderline:") - let menuItems = [playAudioItem, highlightItem, colorsItem, removeItem, yellowItem, greenItem, blueItem, pinkItem, underlineItem, shareItem] + let menuItems = [playAudioItem, highlightItem, defineItem, colorsItem, removeItem, yellowItem, greenItem, blueItem, pinkItem, underlineItem, shareItem] UIMenuController.sharedMenuController().menuItems = menuItems }