Mencoba memahami ini sendiri karena jawaban lain di sini menyebutkan Text.multilineTextAlignment(_:)
/ VStack(alignment:)
/ frame(width:alignment:)
tetapi setiap solusi memecahkan masalah tertentu. Akhirnya itu tergantung pada persyaratan UI dan kombinasi keduanya.
VStack(alignment:)
Di alignment
sini untuk pandangan batin masing-masing satu sama lain.
Jadi menentukan .leading
akan mengaitkan semua tampilan dalam agar memimpin selaras satu sama lain.
VStack(alignment: .leading, spacing: 6) {
Text("Lorem ipsum dolor")
.background(Color.gray.opacity(0.2))
Text("sit amet")
.background(Color.gray.opacity(0.2))
}
.background(Color.gray.opacity(0.1))
.frame
Dalam frame(width:alignment:)
atau frame(maxWidth:alignment:)
, alignment
adalah untuk konten dalam lebar yang ditentukan.
VStack(alignment: .leading, spacing: 6) {
Text("Lorem ipsum dolor")
.background(Color.gray.opacity(0.2))
Text("sit amet")
.background(Color.gray.opacity(0.2))
}
.frame(width: 380, alignment: .trailing)
.background(Color.gray.opacity(0.1))
Tampilan dalam memimpin masing-masing selaras satu sama lain tetapi tampilan itu sendiri mengikuti selaras masing-masing VStack
.
.multilineTextAlignment
Ini menentukan perataan teks di dalamnya dan dapat dilihat paling baik ketika ada beberapa baris jika tidak tanpa ditentukan frame(width:alignment)
, lebar secara otomatis disesuaikan dan terpengaruh oleh default alignment
.
VStack(alignment: .trailing, spacing: 6) {
Text("0. automatic frame\n+ view at parent's specified alignment\n+ multilineTA not set by default at leading")
.background(Color.gray.opacity(0.2))
Text("1. automatic frame\n+ view at parent's specified alignment\n+ multilineTA set to center")
.multilineTextAlignment(.center)
.background(Color.gray.opacity(0.2))
Text("2. automatic frame\n+ view at parent's specified alignment\n+ multilineTA set to trailing")
.multilineTextAlignment(.trailing)
.background(Color.gray.opacity(0.2))
}
.frame(width: 380, alignment: .trailing)
.background(Color.gray.opacity(0.1))
Tes dengan kombinasi:
VStack(alignment: .trailing, spacing: 6) {
Text("1. automatic frame, at parent's alignment")
.background(Color.gray.opacity(0.2))
Text("2. given full width & leading alignment\n+ multilineTA at default leading")
.frame(maxWidth: .infinity, alignment: .leading)
.background(Color.gray.opacity(0.2))
Text("3. given full width & center alignment\n+ multilineTA at default leading")
.frame(maxWidth: .infinity, alignment: .center)
.background(Color.gray.opacity(0.2))
Text("4. given full width & center alignment\n+ multilineTA set to center")
.multilineTextAlignment(.center)
.frame(maxWidth: .infinity, alignment: .center)
.background(Color.gray.opacity(0.2))
Text("5. given full width & center alignment\n+ multilineTA set to trailing")
.multilineTextAlignment(.trailing)
.frame(maxWidth: .infinity, alignment: .center)
.background(Color.gray.opacity(0.2))
Text("6. given full width but no alignment\n+ multilineTA at default leading\n+ leading is based on content, looks odd sometimes as seen here")
.frame(maxWidth: .infinity)
.background(Color.gray.opacity(0.2))
}
.frame(width: 380)
.background(Color.gray.opacity(0.1))