13
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【SwiftUI】@ScaledMetricを使ってフォントサイズを柔軟に対応する

Posted at

はじめに

@ScaledMetricという機能がiOS14から使えるようになっています
通常、サイズに関する値を数値で指定すると固定の値になってしまうところ、@ScaledMetricを使用して指定するといい感じにサイズ変更してくれます

実装

import SwiftUI

struct ContentView: View {
    @ScaledMetric private var fontSize1: CGFloat = 20
    
    private var fontSize2: CGFloat = 20

    var body: some View {
        VStack(spacing: 50) {
            Text("動的サイズ")
                .font(.system(size: fontSize1))

            Text("固定サイズ")
                .font(.system(size: fontSize2))
        }
    }
}

サイズの変化

IMG_4790.PNG IMG_4791.PNG
IMG_4792.PNG IMG_4793.PNG
IMG_4794.PNG IMG_4795.PNG
IMG_4796.PNG IMG_4797.PNG
IMG_4798.PNG IMG_4799.PNG
IMG_4800.PNG IMG_4801.PNG
IMG_4802.PNG IMG_4803.PNG

おわり

指定した値を基準にDynamicTypeに対応してくれるので便利です

13
10
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
13
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?