Combine multiple Text views together in SwiftUI

In this tutorial, you are going to see how to combine multiple text views in SwiftUI together so that they can be treated as a single one.

SwiftUI allows using the + operator between two or multiple text views in order to concatenate or combine them. This feature can be helpful if you want each text of text views in a different format. You can attach a different modifier to give them different styles and looks.

Let’s see the simplest example of combining two separate text views:

Text("Hello ") + Text("World")

You can also break the above code and it will become within two lines:

Text("Hello ")
+ Text("World")

Both of the above codes are similar. They will give the result as shown below:

Hello World Text

Now let’s see another example of adding multiple text views:

        Text("I ")
        + Text("Love ")
        + Text("Swift")

In the above code, I have added the font modifier for the last text view to make the text Swift larger. Below is how the above code will look like:

I Love Swift text

Below is given an example, where we set different fonts, colors and font-weight for each text view:

        Text("Apple devs ")
        + Text("Love ")
            .font(.system(size: 24))
        + Text("Swift")

Below is how the above code will give the result on an iOS device:

I Love Swift text in different style