Cra*_*zed 10 nsattributedstring nsmutableattributedstring swift swiftui
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
}
Run Code Online (Sandbox Code Playgroud)
当我在 SwiftUI 视图中使用上述内容时,如下所示:
var body: some View {
Text(attributedString)
}
Run Code Online (Sandbox Code Playgroud)
我上面嵌入的图像没有显示。所有其他文本均按预期呈现。没有catch
被调用。该图像位于资产中,我也尝试过系统图像。
如果我将相同的属性字符串设置为attributedText
a 的属性,则相同的逻辑将起作用UILabel
。任何帮助表示赞赏。
我也在研究如何在 swiftUI 中做到这一点,我发现了这篇文章
https://swiftuirecipes.com/blog/insert-image-into-swiftui-text
这是如何使用它的示例:
(Text("This image")
+ Text(Image("icon")) // HERE
+ Text("is our logo."))
.font(.title)
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助
归档时间: |
|
查看次数: |
635 次 |
最近记录: |