I’m developing a SwiftUI app, where I want to insert inline image into Text
view. For static content, I may use string interpolation like this:
Text("Hello, (Image(systemName: "pencil")) World! (Image(systemName: "pencil.circle"))")
The result would look like:
But in my case, the content may be dynamic. That is, I may receive the following string from server:
Hello #image(pencil) World! #image(pencil.circle)
I must parse the content dynamically and present the inline image with SwiftUI Text. Is this possible? I only wonder how to construct the string interpolation with image dynamically, the parsing of string is not my concern.
2
Answers
You can pass a variable to the
Image
when you initialize it:Update: Example of using many
Text
items:This is quite an interesting problem!
Here is an alternate way of achieving this.
Input
Hello #image(pencil) World! #image(pencil.circle)
Output
I hope you find the answer helpful.