SwiftUI的Text
不能与NSTextAttachment
一起使用吗?我有这个AttributedString
:
var attributedString: AttributedString {
var sampleAttributedString = AttributedString(sampleText)
// Apply some bold and italics attributes to sampleAttributedString
...
// create NSMutableAttributedString
let mutableAttributedString = NSMutableAttributedString(sampleAttributedString)
let image1Attachment = NSTextAttachment()
image1Attachment.image = UIImage(named: "audio.png")
let imageString = NSAttributedString(attachment: image1Attachment)
mutableAttributedString.append(imageString)
mutableAttributedString.append(NSAttributedString(string: "End of text"))
sampleAttributedString = AttributedString(mutableAttributedString)
return sampleAttributedString
}
当我在SwiftUI视图中使用上述代码如下:
var body: some View {
Text(attributedString)
}
我嵌入的图片没有显示出来。所有其他文本都按预期呈现。
catch
没有被调用。图片在资源中,我也尝试过使用系统图片。如果我将相同的属性字符串设置为
UILabel
的 attributedText
属性,相同的逻辑就会起作用。感谢任何帮助。