Instruction 01
You can choose how Writing Tools appear on UIKit text views and also on web views.
Like UITextView, WKWebView supports Writing Tools by default. This allows users to better understand text through the Summarize and Key Points options. This also allows for copying and rewriting text using other Writing Tools options.
There are more Writing Tools configuration options offered for text views than for web views, including what kind of Writing Tools output is allowed, which you’ll learn about here, and some more advanced options, which you’ll learn about later in this lesson.
Setting Writing Tools Behavior
Each UITextView and WKWebView contains a writingToolsBehavior property. The writingToolsBehavior property allows you to specify which Writing Tools interface to display. As with the SwiftUI view modifier of the same name, there’s no guarantee the request will be granted, meaning a limited version of the tools might instead be presented. The .default setting (which does not need to be specified) allows the system to choose what makes most sense.
If you wish to provide no behavior and thus suppress Writing Tools in a view, you can do the following:
textView.writingToolsBehavior = .none
The UIWritingToolsBehavior enum offers four options in all:
- default: Allows the system to choose the best option for the current view.
- none: Disables Writing Tools on this view.
- complete: Provides the full experience, including the in-line toolbar on iOS and iPadOS.
- limited: Offers just an overlay panel experience.
You can get as well as set Writing Tools behavior, meaning you can read the current Writing Tools behavior of a UITextView or WKWebView.
Note: for the
.completeexperience to appear on a UITextView, that view must use TextKit 2. If a view uses TextKit 1, only the.limitedexperience will be available.
Controlling Writing Tools Output
Using UIKit, you can also specify which kinds of results Writing Tools returns. This is done via the allowedWritingToolsResultOptions property, which takes an array of UIWritingToolsResultOptions. You can both get and set this property.
In the following example, Writing Tools can generate plain and rich text but should not return lists or tables. Other options include list and table, both of which imply rich text attributes will be preserved.
textView.allowedWritingToolsResultOptions = [.plainText, .richText]
Note: By default, Writing Tools transformations preserve formatting. For example, if an italicized word from the original draft is retained in the suggested rewrite, it should still appear in italics.