【Xcode/SwiftUI】自動音声でテキストを読み上げる方法

import SwiftUI
import AVFoundation

struct HomeView: View {
    @State private var text = ""
    private let speechSynthesizer = AVSpeechSynthesizer()

    var body: some View {
        VStack(spacing: 20) {
            Text("読み上げ内容")
                .font(.system(size: 16, weight: .medium))
            Text(text)
                .font(.system(size: 24, weight: .bold))
            TextField("Enter text", text: $text)
                .textFieldStyle(RoundedBorderTextFieldStyle())
                .padding()
            Button(action: {
                speakButtonPressed()
            }, label: {
                Text("Speak")
                    .frame(width: 160, height: 40)
                    .font(.title)
                    .padding()
                    .background(Color.orange)
                    .foregroundColor(.white)
                    .cornerRadius(10)
            })
        }
        .padding()
    }

    private func speakButtonPressed() {
        let speechUtterance = AVSpeechUtterance(string: text)
        speechSynthesizer.speak(speechUtterance)
    }

}

struct HomeView_Previews: PreviewProvider {
    static var previews: some View {
        HomeView()
    }
}

この記事は役に立ちましたか?

はい
いいえ
貴重なフィードバックありがとうございます!