1
3

【SwiftUI】Textの行間スペースを調整する

Posted at

はじめに

Textの行間スペースを調整するという内容です。
どこのことだ?と思うかたもいると思うので画像で説明します。

スクリーンショット 2024-01-24 21.54.45.png

改行された文と文の間のスペースを本記事では行間スペースと表現しています。

サンプルアプリ

Simulator Screen Recording - iPhone 15 - 2024-01-24 at 21.57.08.gif

サンプルアプリの実装

import SwiftUI

struct ContentView: View {
    @State private var lineSpacing: CGFloat = 0.0
    
    var body: some View {
        VStack {
            Text("""
                 あああああああああああああああああ
                 あああああああああああああああああ
                 あああああああああああああああああ
                 あああああああああああああああああ
                 """)
+           .lineSpacing(lineSpacing)
            
            Stepper(value: $lineSpacing) {
                Text("lineSpacing: \(lineSpacing)")
            }
        }
        .padding(20)
        
    }
}

おわり

Textのheightを小さくしたいときに使えそうですね

公式ドキュメント

1
3
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
3