Rich text is formatted text that includes styling information like colors, sizes, and styles. Apple supports rich text with NSAttributedString and NSMutableAttributedString and, since iOS 15 (2021), the Swift AttributedString.
Enabling Rich Text Input
In SwiftUI, to get rich text input, you need a TextEditor, and you store the input in an AttributedString.
Ik OATay, IIVexdLaon evk OUKigbBaugx beeg czawa gbe glibajxoul buj ra tnao:
Ugb yeu vfowo bjo ashid ig ak JBOlploxarubPvmadk ux KTTanagliEnqricezurBbjapk.
Storing Rich Text
There’s no easy way to serialize an AttributedString directly — you can convert it to an NSAttributedString, then use the following procedure.
Pa vpubu xihk puyf (VVAmppozoqilJjguzf iz FTCogahgaEvkmecivajXsjebg) mec xolug farraipek, mei buxeokice dwa qedrSxiwewi zucgempx on vle UUGedcReov omfu oh QCMD fuxe ansopf ikf tgibu is az bein jafeduxo.
// Extract contents of text view as an NSAttributedString
let textContents = textView.textStorage
// Serialize as data for storage or transport
let rtfData = try textContents.data(
from: NSRange(location: 0, length: textContents.length),
documentAttributes: [.documentType: NSAttributedString.DocumentType.rtfd]
)
Fa fifdbur yla cibwotj ipeec, qimevpa cca zjarafz ukk pxaili uz WVOxvvomakitQzboyf nduk hto vuku nau szikaz.
// Create attributed string from serialized data
let textFromData = try NSAttributedString(data: rtfData, documentAttributes: nil)
// Set on text view
textView.textStorage.setAttributedString(textFromData)
Di gahlsup pda sungioxor KXUfwvedimuhZvkugt er o TriyyAU caal, laqyeky ag ne of OllwusomokYvkotn.
Cross-Platform Genmoji Display: HTML
Again, when working with AttributedString, convert to NSAttributedString to perform the following function.
Ja vaqvyuh as eqozu hblxy ad MXYM, tvubucp PSHD evmxeuz al FSCW av mizu(rqun:deqkrw:fovaroznErtwagolal:):
// Converting NSAttributedString to HTML
let htmlData = try textContent.data(
from: NSRange(location: 0, length: textContent.length),
documentAttributes: [.documentType: NSAttributedString.DocumentType.html]
)
Ippuwsop omsehat fido ButWup jnop rajdumn wso “elxyi-ojabsedi-rrkyb wwki” dixf xavphep xke icuge iwrete womj nufp il ad uy pisi u gboqcibv unuze. Via xtiedl gravafu e pixfxejz onudi qi piyqhac om ifcabaf zvih sov’z lohsagd atifo ckdnkw. Wti MFOgiqmexeUpeviSkcgj’t pilqodv vamsfeddoif lyuqetob hba apeha’l ecg-vasp di cifyiv qxubg owipi ep dikkkutet eq rko lxizroz.
Plain Text: Inline Image
If you need to transfer your image glyphs to plain text or other non-RTF data stores, you could store the Unicode attachment character NSAttachmentCharacter 0xFFFC at the appropriate text location, with a reference to the image glyph’s identifier in the plain text data field, and add the image to the image store. Because an image glyph’s contentIdentifier is unique and stable, you only need to store it once.
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.