Home | Anecdotes | Articles | Icebreakers | Programs | Quotations | ShopDashboard | Log in | Sign up

Working with the SwiftUI TextEditor

How to read and edit the content of the SwiftUI TextEditor

I have written a MacOS app to check the readability of my writing. It works by pasting a passage of at least 100 words into the TextEditor window and clicking the 'Calculate' button. The results are shown beneath the window with the 'Reading Age' giving an overall indicator of the passage's readability.

In this article, I will be covering:

  1. Reading the content of the text editor and
  2. Editing the content.

Reading the TextEditor

The readability of the of the passage is calculated by using the:

To do this, the ‘calculate’ function needs to be able to read the passage that had been pasted into the text editor.

This is done by declaring an ‘@State’ variable at the beginning of the ContentView…

@State private var passage: String = ""

…and inserting this variable into the TextEditor code:

TextEditor(text: $passage)  
  .cornerRadius(5.0)  
  .font(.system(size: 14))

The ‘calculate’ function uses the ‘passage’ variable to provide the data it needs to calculate the reading age. Changing the passage variable should cause an update to the view.

Writing to the TextEditor

With this app, I wanted to be able to clear the window and results by clicking a ‘Reset’ button.

When I tried doing this by calling the ‘reset’ function from the ‘Reset’ button, it didn’t work:

Button(action: reset) {  
  Text("Reset")  
}  
.padding(.all, 0.0)

I solved this by changing the ‘passage’ before calling the reset function:

Button("Reset") {  
  passage = ""  
  reset()  
}  
.padding(.all, 0.0)

You can change the ‘passage’ variable to any string you want. This is how you edit the SwiftUI TextEditor.

The following code is, with the exception of the code for calculating the Reading Age, is a complete example of how to use the SwiftUI TextEditor:

import SwiftUI

struct ContentView: View {
  @State private var passage: String = ""
  @State private var strOutput: String = "Number of words: nNumber of sentences:  nAverage sentence length: nNumber of long words:  nnReading Age:  nAI Score (beta):"

  func calculate() {
    //
    Code for calculating
    the reading age
    //
    strOutput = "Number of words: " + strNumberWords + "nNumber of sentences: " + strNumberSentences + "nAverage sentence length:  " + strAvSentenceLength + " wordsnNumber of long words:  " + strLongWords + " (" + strPercentLongWords + " per cent)" + "nnReading Age:  " + strReadingAge + " (Fog Index: " + String(fogIndex) + ")nAI Score (beta): " + strAiScore
  }


 func reset() {
  strOutput =  "Number of words: nNumber of sentences:  nAverage sentence length: nNumber of long words:  nnReading Age:  nAI Score (beta):"
  }

  var body: some View {
    VStack(
      alignment: .leading, spacing: 10) {

      Text("Paste a passage of at least 100 words into the box below and click the calculate button.")  
        .font(.system(size: 16, weight: .light))

      TextEditor(text: $passage)  
        .border(Color.gray)  
        .cornerRadius(5.0)  
        .font(.system(size: 14))

      HStack {  
        Button(action: calculate) {  
          Text("Calculate")  
        }  
        .padding(.all, 0.0)

        Button("Reset") {  
          passage = ""  
          reset()  
        }  
        .padding(.all, 0.0)  
      }

      Text(strOutput)  
      .padding(.leading, 0.2)  
    }  
    .padding(.all, 10.0)
  }
}

Conclusion

This article is an example of what I have learned when creating programs. Now it has been written and published, I can refer back to it when I need to use the SwiftUI Text Editor again.

I hope you also found this useful. Please look out for other articles about my learning journey.

🍯 Leave a tip

More Programming articles

126   127


About Learning Pages | Support us | Latest Resources