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.
Reading the TextEditor
The readability of the of the passage is calculated by using the:
- number of words,
- number of sentences,
- average sentence length and
- number of long words.
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)
.border(Color.gray)
.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’ variable 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 can 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: \n\nReading 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 + " words\nNumber of long words: " + strLongWords + " (" + strPercentLongWords + " per cent)" + "\n\nReading 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: \n\nReading 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.
You can buy the Readable reading age calculator at the Learning Pages Digital Shop. Payment can be made by card, PayPal, HBD or BCH.
Comments (3)
Dear @learningpages this is a great guide. I wanted to ask you how you improved the discoverability of your website in the search engines. Are you actively creating backlinks for your website or maybe just focussing just on the content?
I'm not worried about discoverability because I have decided to take a laidback attitude and just see what happens.
Yeah you told me before that you have a subscription based model setup on your website but the question is are you making any money from this present model? And do you have a paid email sender service like ConvertKit or maybe MailChimp?
Voted through #Ecency. Curator - Guest
Congratulations @learningpages! You have completed the following achievement on the Hive blockchain And have been rewarded with New badge(s)
Your next target is to reach 2250 upvotes.
You can view your badges on your board and compare yourself to others in the Ranking If you no longer want to receive notifications, reply to this comment with the word
STOP
Check out our last posts: