1

I can combine two text views like this.

Text("1") + Text("1")

But how can I do something like this?

Text("1") + Text("1").border(Color.red)

I cant simply do this

HStack(spacing: 0) { 
    Text("1")
    Text("1").border(Color.red) 
}

because the design will behave differently as seen below enter image description here

They will behave as 2 separate texts. I want the one on the right to wrap around, so that both Texts looks like a single text.

Just a coder
  • 15,480
  • 16
  • 85
  • 138

1 Answers1

0

operator + could be applied if both operands are the same type (Text in your case).

struct MyView: View {

    var body: some View {

        Text("A").font(.largeTitle).foregroundColor(Color.red) + Text("B").font(.title) + Text("C").font(.footnote)
    }
}

gives you

enter image description here

which is exactly one Text again, just because .font or .foreground modifier could return Text if applied on Text.

.border modifier returns View, that is why it doesn't work in your example.

user3441734
  • 16,722
  • 2
  • 40
  • 59
  • But is there a way to add a background color to one of those texts? – Just a coder Apr 03 '20 at 14:03
  • yes, just calculate each size, and make background View as composition of different rectangles ... be inspired here https://stackoverflow.com/questions/59832193/how-to-get-position-of-each-character-in-swiftui-text – user3441734 Apr 03 '20 at 14:18