How to insert images into text
About 2 min
How to insert images into text êŽë š
SwiftUI by Example
Back to Home
How to insert images into text | SwiftUI by Example
How to insert images into text
Updated for Xcode 15
SwiftUI lets you combine text views using +
, but you can also place images directly into text using a simple Text
initializer. This allows you to place images directly inside text, including having the text and images wrap as needed.
For example, this writes âHello Worldâ with a star image in the middle:
Text("Hello ") + Text(Image(systemName: "star")) + Text(" World!")
The images inside your text will automatically adjust to match whatever font or foreground color youâve chosen, but make sure you apply your modifiers to the whole joined text rather than simply the last item.
For example, this will make the whole combined text large and blue:
(Text("Hello ") + Text(Image(systemName: "star")) + Text(" World!"))
.foregroundStyle(.blue)
.font(.largeTitle)
Whereas this â without the extra parentheses â will make only the âWorldâ text large and blue:
Text("Goodbye ") + Text(Image(systemName: "star")) + Text(" World!")
.foregroundStyle(.blue)
.font(.largeTitle)
Similar solutionsâŠ
How to use images and other views as backgrounds | SwiftUI by Example
How to use images and other views as backgrounds
How to let the user paste data into your app | SwiftUI by Example
How to let the user paste data into your app
How to format text inside text views | SwiftUI by Example
How to format text inside text views
How to show different images and other views in light or dark mode | SwiftUI by Example
How to show different images and other views in light or dark mode
How to disable the overlay color for images inside Button and NavigationLink | SwiftUI by Example
How to disable the overlay color for images inside Button and NavigationLink