如何在较小的设备上使用较小的字体大小?

huangapple go评论82阅读模式
英文:

How to have smaller font sizes for smaller devices?

问题

我正在开发一个应用程序,并使用硬编码的字体大小。在iOS模拟器上,它在iPhone 14及更高版本上看起来如预期,但在iPhone SE上尝试时,字体太大了。

如何根据设备处理不同的字体大小?

在考虑这个问题时,我唯一能想到的是使用GeometryReader来检查显示屏的高度/宽度,然后有条件地选择字体大小,但这似乎有点混乱。有没有更好的处理方法?

英文:

I am developing an app and using hardcoded font sizes. In the iOS simulator it looks as expected on an iPhone 14 and above but when I tried it on an iPhone SE, it's too large.

How can I handle different font sizes based on the device?

When thinking about it, all I could really think of is using a GeometryReader to check the display height/width and then conditionally select a font size but that seems a little messy. Is there a better way to handle this?

答案1

得分: 2

这是一个极其不良的做法。如果我作为一个视力有障碍的用户,将字体大小调大以便阅读,请不要与我作对。

根据您所在的地区和市场领域,您甚至可能违反了无障碍法律。

重新设计您的界面,以适应动态文本和不同设备尺寸。

英文:

This is an extraordinarily bad practice. If I, as a low vision user, turn up my font size to something I can read, please don’t fight me.

Depending on your location and market sector, you might even be violating accessibility laws.

Redo your design to adapt to dynamic text and varying device sizes.

答案2

得分: 1

你可以使用 @Environment 属性包装器来访问设备的大小类别,它基于用户的字体大小设置。使用此属性来动态调整字体大小。

struct ContentView: View {
    @Environment(\.sizeCategory) var sizeCategory
    
    var body: some View {
        Text("Hello, World!")
            .font(.system(size: getSize()))
    }
    
    func getSize() -> CGFloat {
        switch sizeCategory {
        case .extraSmall, .small:
            return 12
        case .medium:
            return 16
        case .large:
            return 20
        case .extraLarge, .extraExtraLarge, .extraExtraExtraLarge:
            return 24
        default:
            return 16
        }
    }
}
英文:

You can use the '@Environment' property wrapper to access the device's size category, which is based on the user's font size settings. Use this property to adjust your font sizes dynamically.

struct ContentView: View {
@Environment(\.sizeCategory) var sizeCategory

var body: some View {
    Text("Hello, World!")
        .font(.system(size: getSize()))
}

func getSize() -> CGFloat {
    switch sizeCategory {
    case .extraSmall, .small:
        return 12
    case .medium:
        return 16
    case .large:
        return 20
    case .extraLarge, .extraExtraLarge, .extraExtraExtraLarge:
        return 24
    default:
        return 16
    }
  }
}

huangapple
  • 本文由 发表于 2023年2月27日 12:26:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75576774.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定