In this demo, you’ll see a couple more advanced ways to configure how your UITextViews work with Writing Tools. Open the starter project for Lesson Two and select CustomTextView.swift, which is the UIKit version of the cover letter drafting view.
Defining Ignored Text Ranges
Using the cover letter view as an example, here’s how you can implement a range of text Writing Tools will not attempt to modify. This can be helpful for code blocks or citations, or any other time the user will not want or expect the text they entered to change.
Here, you’ll implement this so the salutation “Dear Hiring Manager,” remains untouched by the tools.
Add the following method to the Coordinator class in CustomTextView.swift, just below the declaration of textViewDidChange():
func textView(_ textView: UITextView, writingToolsIgnoredRangesIn
enclosingRange: NSRange) -> [NSRange] {
}
You’re implementing the textView(_:writingToolsIgnoredRangesIn:) method, which takes a textView and an enclosingRange, which is of type NSRange. enclosingRange specifies a range of text in the UITextView. This method returns an array of NSRange objects indicating the parts of the text that Writing Tools should ignore.
Add the following in the method body:
let fullText = textView.text as NSString
Here, you convert the text in the view into an NSString and store it in fullText.
let searchString = "Dear Hiring Manager,"
let range = fullText.range(of: searchString)
Next you create a variable called searchString. This holds the text you want the Writing Tools to ignore. Then you use the range method on this NSString to find the searchString in it.
if range.location != NSNotFound {
return [range]
} else {
return []
}
If the text was found, this line returns an array containing the range. This tells Writing Tools to ignore the text in this range. Otherwise, an empty array is returned, indicating there are no ranges to ignore. This means Writing Tools can modify any part of the text.
Preventing Unexpected Data Loss or Changes
Next, you’ll see how to use textViewWritingToolsWillBegin() and textViewWritingToolsDidEnd() to manage app operations when Writing Tools are active.
To see this in action, you’ll update the Coordinator class in CustomTextView.swift to make sure the user’s cover letter draft gets saved as expected even when they accept changes from Writing Tools.
First, add the following just below the declaration of parent near the top of the class:
var textWhenWritingToolsBegins = ""
This property begins as an empty string. You’ll see in just a moment how it’s helpful.
Next, add the following implementation at the end of the Coordinator class:
func textViewWritingToolsWillBegin(_ textView: UITextView) {
textWhenWritingToolsBegins = textView.text
}
This safely stores the view’s text into textWhenWritingToolsBegins so it can be restored as expected later.
Finally, just below that, add the following:
func textViewWritingToolsDidEnd(_ textView: UITextView) {
if textView.text != textWhenWritingToolsBegins {
}
}
This method runs when the tools finish their work. It first checks to see whether the current text in the view differs from what was stored in textWhenWritingToolsBegins right before the tools ran. If so, that means the user accepted changes suggested by the tools and now the updated draft needs to be saved. Add the following to the if block:
var updatedLetter = parent.coverLetter
updatedLetter.content = textView.text
parent.coverLetterManager.saveCoverLetter(updatedLetter)
print("Updated letter saved")
Here you create a new cover letter object, updatedLetter. You set its content to the text in the view, and call saveCoverLetter() to persist it. You also print a message to this effect to the console. Now for the else block:
} else {
textWhenWritingToolsBegins = ""
print("No changes to save")
}
Here the user didn’t make any changes. That is, they chose to Revert to their original text. There’s no need to do anything else here because there are no changes to persist. Here, too, a message prints to the console.
Build and run, choose Craft Cover Letter, and enter some text. Try Proofread and choose Done to accept the changes. You should see Updated letter saved appear in Xcode’s console. Try a second time, but do not accept the changes, and you’ll see No changes to save in the console and the draft letter saved in Saved Drafts.
OK, admittedly this is a simplistic and somewhat contrived example. Most likely, you’ll use these methods when syncing with cloud storage. But at least now you know they’re there and you’ve gotten a taste of how they work.
Congratulations! You’ve made it almost to the end of the Writing Tools module. Continue to the next segment to review what you’ve learned and then try your hand at the quiz. Enjoy configuring your text views to let users take advantage of Writing Tools intelligently!