1
1

More than 3 years have passed since last update.

[SwiftUI]Textの右寄せが難しい

Posted at

このようなTextの右寄せが意外と分かりにくく難しかった。

スクリーンショット 2020-09-01 19.19.48.png

うまくいったコード

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)
のように幅固定で右寄せにしました。

参考

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1