0

1ForEach loop displays words separated in different lines and not keeping words in original text form. whether in HStack or VStack still there is same issue.

HStack words VStack words

import SwiftUI

struct ContentView: View {
    
    let mainText = "In the midst of a wartime evacuation, a British aeroplane crashes on or near an isolated island in a remote region of the Pacific Ocean."
    
    let wordsToMakeTapable = ["evacuation,", "crashes", "isolated"]
    let definitions = ["evacuation,": "make empty", "crashes": "hit", "isolated": "far away"]
    
    
    var body: some View {
        
        let words = mainText.components(separatedBy: " ")
        
        HStack {
            ForEach(words, id: \.self) { word in
                if wordsToMakeTapable.contains(word) {
                    Text(word)
                        .foregroundColor(.blue)
                        .bold()
                        .onTapGesture {
                            switch word {
                            case "evacuation":
                                print(definitions["evacuation"]!)
                            case "crashes":
                                print(definitions["crashes"]!)
                            case "isolated":
                                print(definitions["isolated"]!)
                            default:
                                break
                            }
                        }
                } else {
                    Text(word)
                }
                Spacer()
                
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

please let me know how to fix it.

Beryl
  • 1
  • 1

0 Answers0