我正在尝试用AttributedString
替换NSAttributedString
,但是没有成功地使附件生效。尽管我应用了附件,但是图像没有出现在字符串中。
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)
1条答案
按热度按时间n3schb8v1#
NSAttributedString(attachment:)
神奇地创建了一个包含单个字符的NSAttributedString
(NSAttachmentCharacter
是U+FFFC OBJECT REPLACEMENT CHARACTER),并应用了text attachment属性,以便用图像替换该字符。使用新的
AttributedString
API,您需要手动复制:下面是一个用图像替换子字符串的示例: