如何在 PDF 中获取选定的单词,以便我可以读出该单词? [Swift, PDF套件]
How can I get a selected word in PDF so that I can have the word pronounced? [Swift, PDFKit]
我正在 Swift 中为 iOS 创建一个 PDF 查看器。
通过该应用程序,我想创建一个功能,让学习者可以点击 PDF 中的每个单词来听到每个单词的发音。
我成功地展示了一个 PDF 文件并朗读了一段文字。但是,我正在努力将它们连接在一起。
如果你能告诉我如何修改以下代码,当用户点击一个词时,该词通过使用文本到语音程序发音,我将不胜感激。
非常感谢!
ViewController.swift
import UIKit
import PDFKit
import AVFoundation
class ViewController: UIViewController {
@IBOutlet weak var pdfView: PDFView!
override func viewDidLoad() {
super.viewDidLoad()
if let url = Bundle.main.url(forResource: "pdf", withExtension: "pdf"){
if let pdfDocument = PDFDocument(url:url){
pdfView.autoScales = true
pdfView.document = pdfDocument
}
let wordTouched = "test"
//playing the word
let utterance = AVSpeechUtterance(string: wordTouched)
utterance.voice = AVSpeechSynthesisVoice(language: "en-US")
let synth = AVSpeechSynthesizer()
synth.speak(utterance)
我知道有几个关于如何 select PDF 中所有文本的讨论 ()。
我想得到一个用户点击的单词,并将其发送到 AVSpeechSynthesizer()
.
将 UITapGestureRecognizer 添加到 pdfView:
let tapgesture = UITapGestureRecognizer(target: self, action: #selector(tapGesture(_:)))
pdfView.addGestureRecognizer(tapgesture)
处理点击手势:
@objc func tapGesture(_ gestureRecognizer: UITapGestureRecognizer) {
let point = gestureRecognizer.location(in: pdfView)
if let page = pdfView.page(for: point, nearest: false) {
//convert point from pdfView coordinate system to page coordinate system
let convertedPoint = pdfView.convert(point, to: page)
//ensure that there is no link/url at this point
if page.annotation(at: convertedPoint) == nil {
//get word at this point
if let selection = page.selectionForWord(at: convertedPoint) {
if let wordTouched = selection.string {
//pronounce word
let utterance = AVSpeechUtterance(string: wordTouched)
utterance.voice = AVSpeechSynthesisVoice(language: "en-US")
let synth = AVSpeechSynthesizer()
synth.speak(utterance)
//if you also want to show selection of this word for one second
pdfView.currentSelection = selection
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
self.pdfView.clearSelection()
}
}
}
}
}
}
我正在 Swift 中为 iOS 创建一个 PDF 查看器。 通过该应用程序,我想创建一个功能,让学习者可以点击 PDF 中的每个单词来听到每个单词的发音。
我成功地展示了一个 PDF 文件并朗读了一段文字。但是,我正在努力将它们连接在一起。
如果你能告诉我如何修改以下代码,当用户点击一个词时,该词通过使用文本到语音程序发音,我将不胜感激。
非常感谢!
ViewController.swift
import UIKit
import PDFKit
import AVFoundation
class ViewController: UIViewController {
@IBOutlet weak var pdfView: PDFView!
override func viewDidLoad() {
super.viewDidLoad()
if let url = Bundle.main.url(forResource: "pdf", withExtension: "pdf"){
if let pdfDocument = PDFDocument(url:url){
pdfView.autoScales = true
pdfView.document = pdfDocument
}
let wordTouched = "test"
//playing the word
let utterance = AVSpeechUtterance(string: wordTouched)
utterance.voice = AVSpeechSynthesisVoice(language: "en-US")
let synth = AVSpeechSynthesizer()
synth.speak(utterance)
我知道有几个关于如何 select PDF 中所有文本的讨论 (AVSpeechSynthesizer()
.
将 UITapGestureRecognizer 添加到 pdfView:
let tapgesture = UITapGestureRecognizer(target: self, action: #selector(tapGesture(_:))) pdfView.addGestureRecognizer(tapgesture)
处理点击手势:
@objc func tapGesture(_ gestureRecognizer: UITapGestureRecognizer) { let point = gestureRecognizer.location(in: pdfView) if let page = pdfView.page(for: point, nearest: false) { //convert point from pdfView coordinate system to page coordinate system let convertedPoint = pdfView.convert(point, to: page) //ensure that there is no link/url at this point if page.annotation(at: convertedPoint) == nil { //get word at this point if let selection = page.selectionForWord(at: convertedPoint) { if let wordTouched = selection.string { //pronounce word let utterance = AVSpeechUtterance(string: wordTouched) utterance.voice = AVSpeechSynthesisVoice(language: "en-US") let synth = AVSpeechSynthesizer() synth.speak(utterance) //if you also want to show selection of this word for one second pdfView.currentSelection = selection DispatchQueue.main.asyncAfter(deadline: .now() + 1) { self.pdfView.clearSelection() } } } } } }