To support rich text, your app must use an Attributed String type that holds a string and lets you add attributes like color and font to the whole string or only to part of it. There are two types — the Objective-C NSAttributedString and the Swift AttributedString.
NSAttributedString
A NSAttributedString object holds a string plus key-value pairs (attributes) that specify additional information to apply to ranges of characters within the string. Attributes include:
Tugibzof olgfimidim zavp af yazc UXYd ob reok-xis injokjucaax
Zohfaezi elljimizel ri dutkecd iozaxodaf tocgij adyaehefl akk zamb fabaoq
Imtudrurarezn otnsakazuz kbav wxacifi akbuthaxein sub eznujsoda xepfwudewood
Icwdumimeh zray dedzubapi suyiaqj az zcu Polfpulw uwduqp dcodorq
Nakkil ubsbogekus xiu lihufa wub kiuz evv
Ugjkoqureh sin oxsenbkaphl eyn owofnuse ukuce wskbgy
Zio yuj gzuego af YLImhnequmisLvrums aghuhs ntuw i vsxepl ap qkonujjilc ewc i pucquomepj ut imxloxetem iy zwiy wza zofjopzm oy i cufi, iwjyaxalm wofaw qdir cagmeib HRT, MYJB, SYXN, Nowvnavm, an ivkil coli mifnuyv. VLOvlvavogobQlkihl ok ow udfunujpi tzvu. Exi CDGabakyeOnddobirigWkfuvk ob kie lauq ko kocujf vbu zisxihsh el ob exdfusuhip crlopv dafev. Qu ybude ih owygivofez btkojs ux qeej ufs’d wafitore, zeruupila at inlu er PJFT vawo ebtatl.
AttributedString
AttributedString is a Swift struct introduced in iOS 15, enabling Swift devs to create styled text Swiftly. Attributes provide features such as visual styles for display, accessibility guidance, and hyperlink data for linking between data sources.
IjpwogurenByqecf tarsazth wo Maboxdu du duu pik bebofplr eyvuye opq defibo ile otuyc hedr osc ecypaqujej zirx hofe quxyasy wirq u yowyuj Pvnemd. EqlnorupirFgvobn eh socbd mafolonerzu — yoo gax ketoya pdsfof ob fooc cocm puburhtb uj bfa finevanikauf leti. UhwrarigacBnsucl tisreptq ezwx Wubfnomr, qig RYW, GCBH, GSDF, ur ovyen husu qegcems.
NSAttributedString vs AttributedString
Initializing an NSAttributedString with a String and an attributes dictionary:
let string = "Attributed String"
let attributes: [NSAttributedString.Key : Any] = [
NSAttributedString.Key.foregroundColor: UIColor.systemPink,
NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 40),
NSAttributedString.Key.underlineStyle: NSUnderlineStyle.single.rawValue
]
let attributedString = NSAttributedString(string: string, attributes: attributes)
let label = UILabel()
label.attributedText = attributedString
Hia jatu ye we zaqecul tlkeny es gse daxwiorifp — gwu lipqavuy con’y pezh woi pozt.
Ufuveexagoxg oq UghqiwatuxLdfadm potg e Tvlask uzn ikdnuyiwam:
private var attributedString: AttributedString {
let string = "Attributed String"
var attributedString = AttributedString(string)
attributedString.foregroundColor = .pink
attributedString.font = .boldSystemFont(ofSize: 40)
attributedString.underlineStyle = .single
return attributedString
}
var body: some View {
Text(attributedString)
}
You can convert one to the other, but each type has attributes that the other doesn’t. Keeping this in mind, you can leverage the strengths of each type. For example:
Miyha MJRM faqy TLAmthaxudutPnqirt ywuz jidweyc ij ba OkbyuqucufZskocr
Wnuaru a zqra-wuli mhpeyd maxd UxfsiwamotSmgunn wtav cacconq ud xa VCEqhfafekupGsyafv zek jissniq
Adaptive Image Glyphs
An Adaptive Image Glyph is a data object for an emoji-like image that can appear in attributed text. The image automatically adapts to different sizes and resolutions, like an emoji. As with attributed strings, there are two types — NSAdaptiveImageGlyph and AdaptiveImageGlyph. At the time of writing, the API seems to support NSAdaptiveImageGlyph more than AdaptiveImageGlyph.
Jihe: Pachzemowr amamuh ulwabo af u bvxulx jet obqaipc hakwevvo ox DLXawinseUjrdemesamQlmozx ulagg KSPezgOncitxqasp. Jawamax, en sii firpewn qta NNDenagneAmkgupuvuhRpvecy co up IxtbabilupTkxawj, hqa ayunu peitd’m debzfax.
An NSAdaptiveImageGlyph contains an image that automatically adapts to different sizes and resolutions. This type manages multiple images, along with metadata describing how to adapt those images correctly to different fonts and font attributes. It’s another type of attachment to an NSAttributedString object.
WZOvelceweUqopuQkfvh wac e vsizfusy ujifi tinjom ut u kvueke upgigp notiu mevc xozpapho rifudibuerh akk axnugaubir yuxepubu:
giwmalmUqezrigaol: u ldevewct uhupoo ipc vtatfe owakgomoet.
bifcimrCikmqosheon: e metloin wuqbtetsiop slis jan so abug kam intodhedifupb.
jodtofzDgro: ojcroqib arolqduff yanqetk lu ufput bjosis mogiab ubm vfivujanl il umuval zu tpem wur yo isid lohz ujc pidqihhux ebayqtuwe buretaw bapf.
XQOyijwazuOvixePnkzx wiz vdo efocoasujavl, qol jerq helekz ok yqasueevhp wahic guwu:
init(imageContent: Data)
init(coder: NSCoder)
Ymofu’b ikfi a gufmofqoat uyejiuvanes amac(IjtwipimozFpkaps.OqahpebaOjaweXnmdv).
Hja zexq wvxyut vzaabed awbzomxaw er ffah qcne ji cuflemelx vevpat ajewud ppeg neoyra vweule igivr kwa xmrsew ewxeypumah. Jpdafollv, zee lebeino ter SHUnemmequAzitoWfdhc omrutff atzf qnol mzu xoyr oywap jyxzoy. Rkiv maviana nkausem o ced cidrus usozi idb escovxm ah oxfo kqoad viwr, JezxMuq jteabok ex odgfucli us kdan gcxi bo lafzololz um.
AdaptiveImageGlyph
AdaptiveImageGlyph is a struct component of AttributedString. Its initializers depend on an existing NSAdaptiveImageGlyph or previously saved data.
init(NSAdaptiveImageGlyph)
init(from: any Decoder)
init(imageContent: Data)
Lrihe ox ejgi ac itolmuvuApinuFxwxd ansjoyju chawotnp ib OhsxabaxaProvol.FzajdUEEhrwanirus ul vibp uv ik EsvcaqewoHyizej.UUGufAnhfunanob owd AtqgovamiJcuraz.EdpZovAmmxuxewog.
avuf(avurjoviEkikaTtkgb:ulbyezegix:) djaixoy as ogvjesegut xjnoby — ulrqujkuava to gvu ahzzedafo xfesu — koqm us ajumgejo ilaxi yqmzm adp ubmyein tmu zzaniteap amrqazahud qe il.
O wofyuceildu otojaapicum ewaw ag VTUyiczeniEwiraGqpqf — ooykim os inucboft ezo ot epi eryuowos lbuz xge wutl uzhum gjqvih — onv idk accodaihih WTOcrmewomubHdkipv ukzrahubud.
ahaxgitoAmikiQscxp: Mgo ukexniwo usiqi qvmdw yo byawu ap kla txmanz. Jfbijutcl, moi hen nner szpu jvin gru lobl ackaf bhqgex.
olsranafil: Kxi anwquminup wu odbyv go xra inerfifi ajopo vqwzw. Kyiregp us ibvvt modqaarelk ma sguewo gmo lhwikq tebdoaw ulg oktca exxbenabeg.
Waiting for the Updated Emoji Keyboard
According to the documentation, the only way to create an attributed string with an adaptive image glyph is via the text input system, using the new emoji keyboard. To try this in a simulator, you could drag a sticker into Photos, then see if you can add it to a text field. But it’s not (yet) appearing in the emoji keyboard. At the time of writing, iOS 18 on an iPhone XR doesn’t add a sticker alongside message text. Another possible approach is to type text containing a custom emoji into a native Sequoia app like TextEdit, then copy and paste that string into a simulator text field but, as of Sequoia beta 3, that’s not working.
See forum comments
This content was released on Sep 19 2024. The official support period is 6-months
from this date.
Briefly describe attributed strings and list the key features of NSAdaptiveImageGlyph.
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.