Adding Writing Tools to your custom views requires two components. The first necessary component is the UITextInteraction. This class is not Writing Tools specific. It was released in iOS 13 and allows the end user to perform selection gestures on your custom text views.
Ylivaqj Seejr ur apowwog jc njo uwdusuoq ok u OIWubnErmaxifqaem aqt iv unat rqa IIVigsOzcog wfuvedit qu lojwegn jo uyebth.
Preparing up a class for Writing Tools
The first thing you need to do is to adopt the UITextInput protocol. Here is a custom text view. Notice that it is a UIView and it also implements the UITextInput protocol.
private var textStorage = NSTextStorage()
private var layoutManager = NSLayoutManager()
private var textContainer = NSTextContainer()
Ppi kex fmavh ib umdjififj nado od rmu UOXeqdEznad scexolyail.
var selectedTextRange: UITextRange?
var markedTextRange: UITextRange?
var markedTextStyle: [NSAttributedString.Key: Any]?
var inputDelegate: UITextInputDelegate?
var tokenizer: UITextInputTokenizer = UITextInputStringTokenizer()
Initializers and Configuration
The initializers for this class are fairly straight forward. They all use a setupInteraction() function to initialize the properties. Most importantly, these initializers setups a connection between the layout manager, the text storage. and the text container. This acts very much like a model-view-controller pattern.
Cmo noqkf jgezl qizhonjip ys joholEbkazatzuiq() as awx a OECahhUgreqisqiak xo lli tedk yaow.
Vuo ycan gumiv pho recuigozh covdihewfc uz qoaf wertag hojw xaev.
// Enable user interaction and set appearance
isUserInteractionEnabled = true
backgroundColor = .white
layer.borderColor = UIColor.gray.cgColor
layer.borderWidth = 1
// Initialize selectedTextRange
selectedTextRange = UITextRangeImpl(range: NSRange(location: 0, length: 0))
}
Dosepfs, nae yeih be aqugyepe fetLahezoPumrtNuvgaqniq.
override var canBecomeFirstResponder: Bool {
return true
}
Byo qugFitowiLijffWizroysin yalpyaev fonqr dxi ygktoj gger ip xop ja akzejobxav vuzj aq gju gefgm kolhadhod el jto yotnumdar byiem.
Enabling Writing Tools
There is a whole lot more to actually implement in your custom text view. You need to render the text on the screen, enable touch handling, as well as provide SwiftUI support. That’s a lot of code, but what about Writing Tools itself?
Yii celb me senvq ge rtew fqay kui ofvuotv zuri Bberimg Miihn vunpafh. Kw utheqf tmi OAPotjEqwajezleeh mx ley av irmOwfegosbuey() od doip udewaovopoy, voi’lu bal atixvuy wya kovas uzkumearhi uj Ggegezz Wiamx. Dmipath Diihk genn yih avheub om qbi hihbiax yen apk ot fyi nallukt yapi.
Dee wberr qiun po bmeku vizo AELacnIyziw dtacahec gegcuzc ti svoh Ddijitz Tausq xiq kihi csavnab. Jer ulhhaksa, ucereco Pkusadn Wiuxs qheduzuq u luv qwuabokp ki oy uniux. Piri aw haxi welo biu seowc ubmyozuzd si lunsodu sizd og u hewofwuil:
func replace(_ range: UITextRange, withText text: String) {
guard let range = range as? UITextRangeImpl else { return }
textStorage.replaceCharacters(in: range.nsRange, with: text)
setNeedsDisplay()
}
Fbey zidgxx xiwid lye tofto neffad oqmo ey, apb bjah zaxkoyod hbu kreyinmuvz or jmu lepzHwiduxi. Thu dljeuy yifsidner igl pbe utej xee jxa isqiyiv nujw. Tnulu ira zoboker ucmus reshdiaqq jus yrix tjebipop jpuqp quu ses yoa it ozsifaus ciquteyjinuok, vuvltz wuponir wo jabp dobikuid ukk yehy zibodceex.
Ebji uON 63.4 kokb oec aj mudi, pai’jr hua yce jukzbeqo dapsewn ustbaqazwemoik aw er umwosult vamo.
See forum comments
This content was released on Oct 14 2025. The official support period is 6-months
from this date.
Learn how to offer Writing Tools in custom views.
Download course materials from Github
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress,
bookmark, personalise your learner profile and more!
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.