このようなText
の右寄せが意外と分かりにくく難しかった。
うまくいったコード
Text
単体ではできませんでした。ZStackでラップして実現しました。
struct ContentView: View {
var body: some View {
HStack(spacing: 16) {
ZStack {
Text("タイトル")
.frame(width: 120, alignment: .trailing)
.foregroundColor(.gray)
.background(Color.init(white: 0.9))
}
Text("テキスト")
.frame(maxWidth: .infinity, alignment: .leading)
.background(Color.init(white: 0.8))
}
}
}
ポイントは、右寄せしたいTextをZStackの中に入れ、
.frame(width: 120, alignment: .trailing)
のように幅固定で右寄せにしました。