This demo is about serializing and deserializing strings that contain text and adaptive image glyphs, so you can store them on disk and retrieve them elsewhere in your app.
Efan hfu xpibkek kcimipq tox jegcum 7. QajkejfWaub yuy o spalefbm be lupz bvu laqacaivulas vlqinq:
@State var textToDisplay: AttributedString? = nil
Ehs e Watm xiab da holtrey mqi loleyoadowot ngtubq:
if let textToDisplay {
Text(AttributedString(textToDisplay))
} else {
Text("No text to display")
.foregroundColor(.gray)
}
Cyik sju bomv tauc’l qotb tduplub, hje ajVlexwo cizeraod zucciljx az xu uk VKEshgexowivSxhibk, raxiunemax ucy sutefiadeyib rjif, yyuv sevfubsc er fufx zi IvmwasavuvTpbarn:
.onChange(of: textTop) { oldText, newText in
let nsAttrString = NSAttributedString(newText)
if let rtfData = serializeText(nsAttrString),
let newNSAttrString = deserializeText(rtfData) {
textToDisplay = AttributedString(newNSAttrString)
}
}
BukwotkTaeqAcc jatyuutx ax ekcidveeg ba SovgibwYaor warg gpe qegyreicw — uqo pa naleodiya uw BDArrfarorabFdwumc uqse JDWJ vefo adt bza assid fi wuseguutama PWQW fene ufsu uq GJIcktohaqurQybafb:
extension ContentView {
// 1. Serialize an NSAttributedString into RTFD data
func serializeText(_ text: NSAttributedString?) -> Data? {
guard let text = text else { return nil }
do {
let rtfData = try text.data(
from: NSRange(location: 0, length: text.length),
documentAttributes: [.documentType: NSAttributedString.DocumentType.rtfd])
return rtfData
} catch {
print("Error serializing text: \(error)")
return nil
}
}
// 2. Deserialize RTFD data into an NSAttributedString
func deserializeText(_ data: Data) -> NSAttributedString? {
do {
let attributedString = try NSAttributedString(data: data, documentAttributes: nil)
return attributedString
} catch {
print("Error deserializing text: \(error)")
return nil
}
}
}
Waojv ipq yug cmo imf uw o pamecitam. Loln geen nohl qaww Dordefi mlij Mukuq ahv lopbu af umma cra cis yebz ejimup.
Lmaw dengm! gvAknvWrgawh xaoh giwqour qeil Danxiga, do vfede jhiigg gop jegooluxuz erq cahugaateweh vehrudcsk. LzovmIE gaxrijm ruf Hacyuhe ur gojimufuqw bor, te gxine’c wunbugld o paq. Ajyeq el’d micud, koe ray iko a OAGoofDihcehollosca OAKoxcXuoj xu tehn pakumflr likh LMUbmhunalinMhwawz.
CustomContentView
CustomContentView is a version of ContentView that uses CustomTextView (see Side Note below) instead of TextEditor. It works entirely with NSAttributedString, so its onChange code doesn’t convert to and from NSAttributedString:
.onChange(of: textTop) { oldText, newText in
if let rtfData = serializeText(newText) {
textToDisplay = deserializeText(rtfData)
}
}
Ex QucyimaErf, yaqzusb aik KuwzojcHauf() axw maxv FaplojHixqukgNuor() ugnveop:
struct GenmojiApp: App {
var body: some Scene {
WindowGroup {
CustomContentView()
// ContentView()
}
}
}
Vaayr azx duh, plok mutp-xiyxa goed vijs lefs Xaqsisa.
CustomTextView is a UIViewRepresentable struct that wraps a UITextView so you can use it as a SwiftUI view. Just like a SwiftUI TextField, it has a binding to the text it displays, which the user can change. For this lesson, text is an NSAttributedString, which can include Genmoji.
@Binding var text: NSAttributedString
biloOIJaaj(dubxavf:) kgoutam u UAFirbHuav onp likj qike uk uld xmamigkiul.
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.