1

I have Name Max and text. I would like the text to be right after line truncation on the left side (without having whitespace). Is such a thing possible in SwiftUI?

Photo:

enter image description here

The code I am currently using is this:

struct NotificationUser: View {
    var name: String
    var text: String
    
    var body: some View {
        HStack(alignment: .top) {
            Text(name)
            .bold()
            .padding(.horizontal, 5)
            .padding(.vertical, 2)
            .background(Color(.systemGray6))
            .cornerRadius(5)
            
            Text(text)
        }
    }
}
Mojtaba Hosseini
  • 95,414
  • 31
  • 268
  • 278
Markon
  • 741
  • 8
  • 23

2 Answers2

1

Attributed String

You can get help from attributed string that I explained here and it has full access for what you are looking for.


More native SwiftUI

Also, you can just use this hack:

var body: some View {
    ZStack(alignment: .topLeading) {
        Text(name)
            .bold()
            .padding(.horizontal, 5)
            .padding(.vertical, 2)
            .background(Color(.systemGray6))
            .cornerRadius(5)

        Group {
            Text(name) // This goes as a frame holder
                .bold()
                .foregroundColor(.clear)

                + Text("  " + text)
        }
        .padding(.horizontal, 5)
        .padding(.vertical, 2)
    }
}

Result

Result

Mojtaba Hosseini
  • 95,414
  • 31
  • 268
  • 278
0

This is a Tags view I wrote for iOS 13 and above. It's a multiline text field with tags deletable.

https://github.com/lijin88121/TagTextField

enter image description here

Li Jin
  • 1,879
  • 2
  • 16
  • 23