Andrew
Andrew

Reputation: 11362

SwiftUI: how to remove spacing around Text()?

As example I have 3 properties:

var path1FilePath:String = "Src/"
var path2FileName: String = "filename"
var path3Extension: String = ".jpg"

I need to display them with the following way:

HStack {
    Text(status.path1FilePath)
    Text(status.path2FileName).bold()
    Text(status.path3Extension)
}

problem is spacing between Text() views. How to remove them?

enter image description here

Upvotes: 6

Views: 5069

Answers (2)

Andrew
Andrew

Reputation: 11362

There are 2 ways:

Solution 1:

Text(path1FilePath)
    + Text(path2FileName)
    + Text(path3Extension)

but in this way you cannot apply modifiers =(


Solution 2:

HStack (spacing: 0) {
    Text(path1FilePath)
    Text(path2FileName)
        .bold()
    Text(path3Extension)
}
.someTextModifier()

Upvotes: 3

Lineous
Lineous

Reputation: 1782

SwiftUI allows us to combine strings together like Text("Hello ") + Text("World!"), so you can do the same here:

Text(path1FilePath)
    + Text(path2FileName)
    + Text(path3Extension)

SwiftUI Text combination

Alternatively, if you still want or need to use an HStack, just use HStack(spacing: 0) and you'll get the same result.

Upvotes: 7

Related Questions