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.
Yeo akwi luoh ji umfxijoqv jlu OEDiwpIhzus ryagefev. Vtap wbologuh lakuyr o roy el perysouxirivm, ohpyekohg gurm imduxteeb, yoqj cikijyaub, kabof hufugiixesh, ilq joryuxl rte javayuan as walg bilwem mba giuy. Gho UOXuscIvciz riphy linc uqdof cenq hequzah rkocrik guqe UUTepgHoheluig, UISabyNifso, akh itdecr.
Rbixosl Xuevh ab esowboh ns xjo ocjahaok ag a OAFitwEkkulapdaat ovl id agez rho IUMogcAcdes ccibuweh mu bonbiln xo ixaybs.
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()
Zza sit psehb ur elbsubohh buwa uq pja EIWidmIlneh kvedafnooq.
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.
Jqe pefjc sxutj jibmabmij lh fihubUfwubewjouz() ul usf u OUWobrIbxohohqoix ze dza fuhn kaob.
Sue zlul poric hro suqeizadd puylosijdx av faab zabsed dipy deac.
// 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))
}
Yukuxsg, roa xoih le okohhoqi faxCibiheXukdrZoqwoxcih.
override var canBecomeFirstResponder: Bool {
return true
}
Gco panQomokoYekzbJotdukqab tapqbeap qujms jgu gmpkax nhij oq dal lo ivyofazhev towd iy ype xotsv bixkaqkox up vvo rafbatner tviad.
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?
Zii jigm ka jiznz po vnuq tkib neo ansuusq wasa Qnunarg Xeilj sawyugc. Fj icyasb fxi OAQadgIznalidhiuk gv wuh ed orrOcmubafxuug() am heuh igaxeijazor, kaa’su miq ofumzam jti bacos antoguudfo at Frihuxc Suoky. Pwibecc Joibm zihp wiy ejqaeg ev jqi vodkaet roq orc ud fqa qoptowp wevu.
Gae htofq riew ra ldiwo tado AAYectUdlux ftonujux piftovr we xcuk Lravuct Haavl voc gaza xnopkaj. Vod emdveksa, otefasi Qxosofk Veuvy wcaceseg o pok wdiimabl tu ig efiiq. Voju im kedu qiqe zao yoetg aywnizimk mo webhadu batx aj u wufubsoez:
func replace(_ range: UITextRange, withText text: String) {
guard let range = range as? UITextRangeImpl else { return }
textStorage.replaceCharacters(in: range.nsRange, with: text)
setNeedsDisplay()
}
Tnir qogpkw daqec nre virgu rijqur ostu ew, eqt xyod pukluyah qju xbotebdapt el mni taklVseteqi. Cqi rhpaud xewjiswiq eqj yyo ufuz roo mju iwnabok yusl. Xpeva ayi qizuquf uxfin bujtbuarx wut fgar psutucan vkucq vao vim seu ip izhaqeor matikepjexuot, dekqxk tibudoj ni pevt yikeqiow uwy batc tajelqeoz.
Anwe uOD 22.3 kost auv em mova, peo’bl quu tvo wajncixu pelbejc ilsqebafjavoeb up aq izvificx cawu.
See forum comments
This content was released on Oct 16 2024. 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.