Li Fumin
Li Fumin

Reputation: 1453

Why doesn't SwiftUI's Text display tab space under some circumstances?

For example, I have this string for Text: Incoming event 0 111.(There are two tab spaces between 0 and 111.)

When there is enough width space to show it,it is fine.

But if there isn't enough width space to show it in a single line, it wraps into two lines,and the tab spaces disappear magically(At least one of the tab spaces does,if not both of them).

Is this a bug or did I miss something?(It is a macOS App)

Upvotes: 0

Views: 401

Answers (2)

ChrisR
ChrisR

Reputation: 12125

I did a quick check with user input and to me it seems like it's working fine ... isn't that the behavior you want?

enter image description here

            TextField("Input", text: $input)
            
            Text(input)
                .frame(width: 300, alignment: .leading)
                .border(.red)
            
            Text(input)
                .frame(width: 150, alignment: .leading)
                .border(.red)

Upvotes: 0

ChrisR
ChrisR

Reputation: 12125

It works fine if you code tab as \t in the string. Otherwise the tab key is translated into spaces, which will sometimes be ignored in line breaking.

enter image description here

        VStack(alignment: .leading, spacing: 30) {
            
            Text("Incoming event 0\t\t111")
                .frame(width: 300, alignment: .leading)
                .border(.red)
            
            Text("Incoming event 0\t\t111")
                .frame(width: 150, alignment: .leading)
                .border(.red)
        }

Upvotes: 1

Related Questions