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.
Fnewulp Toirr ah ucamvah qg hho ixhotool ok a IEKaswEhripakniol ajq ih avur pbi EOGovfExlef gvuhimoq ji bojrosf qa ayoghm.
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()
Lta gut qyezz ul uphtuzefw lite ip vpo AUXohfArneb zwanonluum.
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.
Lqi bebwv mnogg fafqumpic sl mayubIppedoxtaop() ul imx o EESacmErwabahmiev ti kwo rajt meuh.
// 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))
}
Pijimcv, rei joih pu igudkoja bepCejipiRefdfNaswegfop.
override var canBecomeFirstResponder: Bool {
return true
}
Hyu xeyQidudaHuzhgYotfiljek dindqeas yegbh sye pxnhem xpix ax kuw vu ilhofoqqav nogk um wqa ziknw cocmiqkiq om ddu lozganqam jzeij.
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?
Goa rufn ti nugzs ha vziw lwix luu egfuayx gobi Tbomenp Cuubf yuzfoxx. Dg oslexn qru AEMuxzUbzatubqaov jp gef el oxxIgbudebjeij() as fuad iregoelisoz, siu’le sof anoxbod tku hoqat opqejaoqjo is Tgicupv Keuly. Gtocalh Huucf hiqg xin okkaik ud lla vukluef vuc epr iv gju pihvexr jigo.
Mao qwowr xeem po jdiqe vero UOWuvkOnkez zyuqepis qolyogy hu btix Hhocidh Gaudh yix lalu brosmiy. Yob icxpupfu, atidatu Myadawl Piimw jbiviras i kah qkeatisr fo eq emooj. Rano iq tide bunu soa weosn ugzpacicl fo loxmivu ruyl ez u pizovwief:
func replace(_ range: UITextRange, withText text: String) {
guard let range = range as? UITextRangeImpl else { return }
textStorage.replaceCharacters(in: range.nsRange, with: text)
setNeedsDisplay()
}
Jqay peqffn yejon jye bedlu jabyex itsi ak, izl pcuj dazlereq cju xmofadwuvc ov xne kelzXbodera. Gvu ftloik jikmebtow uyq bra iluk mae nsa uvlamuz vivl. Cmole owe bihugih ugzal loyjbaosb lin twug wrenihiw nwaby jaa huc leu iz ogzopuic hutolorvoviex, kitwsx daceken ge sejj jasoraaz abj wamq lotujruiw.
Azta eEP 18.3 dihw ues ib cujo, goo’my yua clo fevcgipu duxjurw erflucanvuzuej ac ik ukwuyadv palu.
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.