我在多个单元格的UITableViewCell中有一个UITextView: 在单击UIButton上将表单文本转换为语音(TTS)需要所有单元格textView的文本。 UIButton位于UITableViewCell中。 现在,按钮的所有单元格的文本视图文本都没有按文本转换为语音。只需在按钮的
上转换一个textView的文本的UITableViewCell
class HomeTableViewCell: UITableViewCell {
var homeViewCon: HomeTableViewController?
let synth = AVSpeechSynthesizer()
@IBAction func ttsBtnClick(_ sender: Any) {
guard let ttsBtnCheck = sender as? UIButton else {
return
}
if ttsBtnCheck.isSelected {
ttsBtn.setImage(UIImage(named: "tts"), for: .normal)
ttsBtnCheck.isSelected = false
synth.stopSpeaking(at: AVSpeechBoundary.immediate)
} else{
print("ttsBtnClick false ")
ttsBtn.setImage(UIImage(named: "ttsBlue"), for: .selected)
ttsBtnCheck.isSelected = true
textToSpeech()
let point = (sender as AnyObject).convert(CGPoint.zero, to: self.homeViewCon?.homeTableView)
let indexPath = self.homeViewCon?.homeTableView.indexPathForRow(at: point)!
let indexPathRow = indexPath?.row ?? 0
let indexPathSection = self.homeViewCon?.homeTableView.numberOfRows(inSection: (indexPath?.section)!)
if indexPathRow < indexPathSection! {
let newIndexPath = NSIndexPath(row:(indexPath?.row)! + 1, section:(indexPath?.section)!)
self.homeViewCon?.homeTableView.scrollToRow(
at: newIndexPath as IndexPath, at:.bottom, animated: true)
}
}
}
func textToSpeech(){
let name: String = newsTextView.text
let endIndex = name.index(name.endIndex, offsetBy: -3)
let truncated = name.substring(to: endIndex)
let index2 = truncated.range(of: ".", options: .backwards)?.lowerBound
var substring3 = index2.map(truncated.substring(to:))
let utterance = AVSpeechUtterance(string: titleLabel.text!+". "+substring3!)
utterance.voice = AVSpeechSynthesisVoice(language: "en-US")
synth.speak(utterance)
}
}
答案 0 :(得分:1)
请找到工作解决方案以及故事板UI。
Swift 4
//
// TableViewVC.swift
//
// Created by Test User on 06/02/18.
// Copyright © 2018. All rights reserved.
//
import UIKit
import AVFoundation
class customCell: UITableViewCell {
@IBOutlet weak var lblTTS: UILabel!
@IBOutlet weak var btnTTS: UIButton!
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
class TableViewVC: UIViewController {
@IBOutlet weak var tblTTS: UITableView!
var arrTTS = [
"This is first text",
"This is second text",
"This is third text",
"This is fourth text",
"This is fifth text",
"This is sixth text",
"This is seventh text",
"This is eighth text",
"This is ninth text",
"This is tenth text",
"This is eleventh text",
"This is twelfth text",
]
let speechSynthesizer = AVSpeechSynthesizer()
var previousSelectedIndexPath : IndexPath?
override func viewDidLoad() {
super.viewDidLoad()
speechSynthesizer.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@IBAction func btnTTSClicked(sender: UIButton) {
let point = sender.convert(CGPoint.zero, to: self.tblTTS)
let indexPath = self.tblTTS.indexPathForRow(at: point)
if previousSelectedIndexPath == indexPath {
let cell = self.tblTTS.cellForRow(at: indexPath!) as! customCell
speechSynthesizer.stopSpeaking(at: .immediate)
cell.btnTTS.setTitle("TTS", for: .normal)
previousSelectedIndexPath = nil
} else {
if previousSelectedIndexPath == nil {
previousSelectedIndexPath = indexPath
let cell = self.tblTTS.cellForRow(at: indexPath!) as! customCell
cell.btnTTS.setTitle("Speaking", for: .normal)
if speechSynthesizer.isSpeaking {
speechSynthesizer.stopSpeaking(at: .immediate)
} else {
let speechUtterance = AVSpeechUtterance(string: self.arrTTS[indexPath!.row])
DispatchQueue.main.async {
self.speechSynthesizer.speak(speechUtterance)
}
}
} else {
let oldCell = self.tblTTS.cellForRow(at: previousSelectedIndexPath!) as! customCell
oldCell.btnTTS.setTitle("TTS", for: .normal)
let cell = self.tblTTS.cellForRow(at: indexPath!) as! customCell
cell.btnTTS.setTitle("Speaking", for: .normal)
previousSelectedIndexPath = indexPath
if speechSynthesizer.isSpeaking {
speechSynthesizer.stopSpeaking(at: .immediate)
} else {
let speechUtterance = AVSpeechUtterance(string: self.arrTTS[indexPath!.row])
DispatchQueue.main.async {
self.speechSynthesizer.speak(speechUtterance)
}
}
}
}
}
}
extension TableViewVC: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrTTS.count
}
//----------------------------------------------------------------
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "customCell") as! customCell
cell.lblTTS.text = self.arrTTS[indexPath.row]
cell.btnTTS.addTarget(self, action: #selector(btnTTSClicked(sender:)), for: .touchUpInside)
return cell
}
//----------------------------------------------------------------
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 50
}
}
extension TableViewVC: AVSpeechSynthesizerDelegate {
func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didFinish utterance: AVSpeechUtterance) {
speechSynthesizer.stopSpeaking(at: .word)
if previousSelectedIndexPath != nil {
let speechUtterance = AVSpeechUtterance(string: self.arrTTS[previousSelectedIndexPath!.row])
DispatchQueue.main.async {
self.speechSynthesizer.speak(speechUtterance)
}
}
}
}
答案 1 :(得分:0)
问题是,在您单击其中一个按钮时,并非所有单元格都可见,因此当用户在textView中输入文本并滚动文本时会丢失,因此请实现didEndDisplaying
以存储当前textView的文本在全局数组中,单击单元格按钮时循环遍历数组内容,并为每个文本执行tts
optional func tableView(_ tableView: UITableView,didEndDisplaying cell: UITableViewCell,forRowAt indexPath: IndexPath)