Is there a better way of positioning text in SwiftUI, in the example below I am positioning the text in the bottom right corner of a ZStack, it works fine but seems long winded, am I missing a simpler way ... The orange lines are just for debugging, so that the spacers are visible in the view.
CODE
struct DisplayTwoView: View {
var body: some View {
ZStack {
Rectangle().foregroundColor(.blue)
Group {
VStack {
Spacer().frame(width: 5).background(Color.orange)
HStack {
Spacer().frame(height: 5).background(Color.orange)
Text("RABBITS").fontWeight(.black)
}
}
}.padding()
}
}
}
VIEW
To align a text view along the horizontal axis, you need to use . frame() modifier with maxWidth set to . infinity and alignment to the alignment you want.
When SwiftUI's Text views wrap across multiple lines, they align to their leading edge by default. If you want to change that, use the multilineTextAlignment() modifier to specify an alternative, such as . center , or . trailing .
A view that arranges its subviews in a horizontal line.
Try this one (tested with Xcode 11.4 / iOS 13.4)
struct DisplayTwoView: View {
var body: some View {
ZStack(alignment: .bottomTrailing) {
Rectangle().foregroundColor(.blue)
Text("RABBITS").fontWeight(.black)
.padding()
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With