5
4

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 3 years have passed since last update.

デバイスの文字サイズの設定によってデザインが崩れるのを防止する

5
Last updated at Posted at 2023-04-21

例えばこんなのがあったとして

import SwiftUI

struct Hoge: View {
    var body: some View {
        content
            .padding(30)
    }
    
    var content: some View {
        VStack(alignment: .center, spacing: 20.0) {
            Text("ABCDEFGHIJKL".uppercased())
                .font(.largeTitle.width(.compressed))
                .fontWeight(.bold)
            Text("「蚊に刺されると、かゆみを感じるのは唾液が原因である」というのは、実は誤解。蚊が吸った血液を注入するための唾液が原因で、かゆみを引き起こす。")
                .multilineTextAlignment(.center)
                .foregroundColor(.secondary)
                .fontWeight(.medium)
            Button {
            } label: {
                Text("へ〜")
                    .padding(.all)
                    .frame(maxWidth: .infinity)
                    .background(.cyan)
                    .cornerRadius(10)
            }
        }
        .padding(30)
        .background(.blue)
        .cornerRadius(10)
        .frame(maxWidth: 500)
    }
}

struct Hoge_Previews: PreviewProvider {
    static var previews: some View {
        Hoge()
    }
}

image.png

通常の文字サイズではいい感じのレイアウトだけど、
設定で文字を大きくするとレイアウトが崩れたり、文字が見切れたりすることがある。
image.png

ちなみにこの画面はキャンバスの下の方のボタンから選択すれば表示できる。
image.png

XXX largeまではいい感じなので、それ以上大きくならないように、文字サイズを制限したい。

import SwiftUI

struct Hoge: View {
    var body: some View {
        content
            .padding(30)
            // こいつを追加
            .dynamicTypeSize(.xSmall ... .xxxLarge)
    }
    
    var content: some View {
        VStack(alignment: .center, spacing: 20.0) {
            Text("ABCDEFGHIJKL".uppercased())
                .font(.largeTitle.width(.compressed))
                .fontWeight(.bold)
            Text("「蚊に刺されると、かゆみを感じるのは唾液が原因である」というのは、実は誤解。蚊が吸った血液を注入するための唾液が原因で、かゆみを引き起こす。")
                .multilineTextAlignment(.center)
                .foregroundColor(.secondary)
                .fontWeight(.medium)
            Button {
            } label: {
                Text("へ〜")
                    .padding(.all)
                    .frame(maxWidth: .infinity)
                    .background(.cyan)
                    .cornerRadius(10)
            }
        }
        .padding(30)
        .background(.blue)
        .cornerRadius(10)
        .frame(maxWidth: 500)
    }
}

struct Hoge_Previews: PreviewProvider {
    static var previews: some View {
        Hoge()
    }
}

XXX Large以上の文字サイズになっていないことがわかる
image.png

5
4
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
5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?