I'm working to replace NSAttributedString with AttributedString but have been unsuccessful in getting attachments to work. The image doesn't appear in the string despite the fact I applied the attachment.
let textAttachment = NSTextAttachment(image: UIImage(systemName: "exclamationmark.triangle.fill")!)
textAttachment.accessibilityLabel = "Warning"
// Original code
label.attributedText = NSAttributedString(attachment: textAttachment)
// New code
var attributedString = AttributedString()
attributedString.attachment = textAttachment
label.attributedText = NSAttributedString(attributedString)
NSAttributedString(attachment:)magically creates anNSAttributedStringwith a single character (NSAttachmentCharacterwhich is U+FFFC OBJECT REPLACEMENT CHARACTER) and applies the text attachment attribute in order to replace that character with the image.With the new
AttributedStringAPI you'll need to manually replicate that:Here's an example that replaces a substring with an image: