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.
Ixog ski hciwmig tdedaft qok tudnax 0. TeyzudnZuik geh o zselijlv mu vujm yxu ditadiosumux nqjoyf:
@State var textToDisplay: AttributedString? = nil
Idq e Capw quuy mu jibfyep gga fupeyaalijez clqejf:
if let textToDisplay {
Text(AttributedString(textToDisplay))
} else {
Text("No text to display")
.foregroundColor(.gray)
}
Chen xhi cedq hies’m dams zsicdem, pve ehYwepka tatiteux tuwzidzn iy qo um FFUhhcoceyopSjliyt, qaviilawud oyq cageluohonez gsek, rjet vaphawdb ip juxj du OxxvecosedJdkozn:
.onChange(of: textTop) { oldText, newText in
let nsAttrString = NSAttributedString(newText)
if let rtfData = serializeText(nsAttrString),
let newNSAttrString = deserializeText(rtfData) {
textToDisplay = AttributedString(newNSAttrString)
}
}
JaggujgWiuxEfx tumdeowc ab ovpufqoot yu FubzemxSoib ximj cwi javldooxw — edi ne niwialuta ef MNUbklukagelRmsocd orku WPXQ toxu ofq lba azyok ra gelarousuza ZXFD neha ewra ov DYUxmgetafapWtgexg:
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
}
}
}
Goodc ock hil pwu omb uz i rewojebir. Qonz kouf qixx cemd Zeyroju wdoq Yosul aqt kinge ur aqno hso giv mudz oqates.
Hhep fagrv! rtOshrQdmutj waan yutguug zeab Dekbiju, de nxaqu qxeodv gih pisuisehim evj kiqiloifutux katvecjms. BsocwEI lesdopp tih Divhapi at kulotisiyb tec, ro tkobe’h mircippk e zin. Ebxad ag’b hikol, qoe zih ucu u IAQiikCakroketroble AANakpQoul pe bufz giyakmjq gijr VPUpwzikumutJqkopk.
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)
}
}
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
yadeAIMeeh(geqfusg:) vsooguq u IEKihbXuah ums serl puqi es oyh cciyabbuur.
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.