當放置具有足夠文本以使其自動換行的 Text() 視圖時,它們沒有相同的對齊/填充,除非它們具有相似的單詞結構。
我希望有一個解決這個問題的方法,允許每個 Text 視圖具有相同的對齊和填充,以及理解我顯然試圖與之抗爭的 SwiftUI 魔法。
我相信這與 swiftUI 的默認“自動居中”對齊行為有關,但不確定為什么相似長度的句子不會以相同的對齊/填充出現。當我嘗試將一個組應用于文本視圖并應用集體填充或 multiLineAlignment 等時,它似乎并沒有改變輸出。
struct TutorialView: View {
var body: some View {
VStack {
Text("About This App.")
.padding([.top, .bottom] , 20)
.font(.system(size: 30))
Text("This example text is meant to be long enough to wrap because when it is, then the alignment aint what we thought it would be...")
Text("This is meant to wrap and it will look different than the text above. even though it wraps too")
Text("It isn't always clear which will appear to have padding and which will hug the wall")
Text("Literally all of these are differnt...")
Spacer()
}
}
}

uj5u.com熱心網友回復:
方法
- 你是對的,默認對齊是中心
- 添加不同的背景顏色,您將了解它們是如何對齊的。
代碼
我修改了您的代碼只是為了演示布局
struct ContentView: View {
var body: some View {
VStack {
Text("About This App.")
.padding([.top, .bottom] , 20)
.font(.system(size: 30))
.background(.orange)
VStack(alignment: .leading) {
Text("This example text is meant to be long enough to wrap because when it is, then the alignment aint what we thought it would be...")
.background(.green)
Text("This is meant to wrap and it will look different than the text above. even though it wraps too")
.background(.blue)
Text("It isn't always clear which will appear to have padding and which will hug the wall")
.background(.red)
Text("Literally all of these are differnt...")
.background(.yellow)
Spacer()
}
}
}
}

筆記:
- 有時
.fixedSize(horizontal:, vertical:)會派上用場(本例中未使用)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/532913.html
