If you have something like this. You'll see that the bottom views rectangle 1
and rectangle 2
are aligned with one another.
HStack(spacing: 0) {
Rectangle()
.fill(Color.smashFrameStartup)
.overlay(
VStack {
Text("Data").font(.system(size: 100.0))
Text("rectangle 1")
}
)
Rectangle()
.fill(Color.smashOnShield)
.overlay(
VStack {
Text("Data").font(.system(size: 100.0))
Text("rectangle 2")
}
)
}
However as soon as you make the top text longer from "Data"
to "Data 2"
or anything longer, you can see that the top view pushes the bottom view further down. How do I stop the bottom view from being pushed further down no matter the length of the text in the top view?
Here's the updated code and what happens.
HStack(spacing: 0) {
Rectangle()
.fill(Color.smashFrameStartup)
.overlay(
VStack {
Text("Data").font(.system(size: 100.0))
Text("rectangle 1")
}
)
Rectangle()
.fill(Color.smashOnShield)
.overlay(
VStack {
Text("Data 2").font(.system(size: 100.0))
Text("rectangle 2")
}
)
}
Is it possible to position the text view so it doesn't move? I would like to have rectangle 2
to be aligned with rectangle 1
no matter the length of the above text view on either rectangle.
I should also note that if the above text view string is long like below, it'll be truncated which I don't want.
HStack(spacing: 0) {
Rectangle()
.fill(Color.green)
.overlay(
VStack {
Text("Data").font(.system(size: 100.0))
Text("rectangle 1")
}
)
Rectangle()
.fill(Color.red)
.overlay(
VStack {
Text("Data 2 with even more information here").font(.system(size: 100.0))
Text("rectangle 2")
}
)
}
I've also tried adding minimumScaleFactor
with 0.4
and it does show the whole text without it being truncated but it has the same problem where it'll push the bottom text view further down.
HStack(spacing: 0) {
Rectangle()
.fill(Color.green)
.overlay(
VStack {
Text("Data").font(.system(size: 100.0))
Text("rectangle 1")
}
)
Rectangle()
.fill(Color.red)
.overlay(
VStack {
Text("Data 2 with even more information here").font(.system(size: 100.0))
.minimumScaleFactor(0.4)
Text("rectangle 2")
}
)
}
EDIT: Criteria
- Have the position of the two bottom text views in each rectangle be aligned with one another like in the first picture and at that position on the screen.
- The top text view can be of variable length, whether it's 2 or up to 40 characters long, it shouldn't cause the bottom text view to be pushed further down. It should be noted that the length of the string in the top text view can be a different length compared to the top text view in the other rectangle. Despite that, the bottom views shouldn't move down and they should still be aligned with one another.
- If the top view text string starts to become too long, it's ok to change the font size as long as we don't shift the bottom text view. In order to show all of the text.
- The very long string in the top view shouldn't be truncated like in the third picture. I tried fixing this with
minimumScaleFactor
and the truncated problem is fixed like in the fourth picture but as you can see the bottom text view is still shifted.