iTranslated by AI
Supporting Dynamic Type in Text Components
Dynamic Type?
Dynamic Type is a feature that allows users to change the default text size for accessibility purposes. The text size can be adjusted in stages, with up to 12 levels available. By default, Large is selected. Specifications regarding size can be found in the HIG's Typography section.
Handling Dynamic Type in Apps
Supporting Dynamic Type is beneficial for the user experience and, from a developer's perspective, is important for attracting a wider range of users. Below, I will introduce how to handle it for each design pattern.
System Font + Styles
You can support Dynamic Type by using the styles provided by Apple. If your design, including fonts, uses system defaults, this is all you need 🙆♂️
Text("Hello, World!")
.font(.title)
System Font + Custom Size
When using the styles mentioned above, detailed size specification (in pt) is not possible. To specify a size while using system fonts:
Text("Hello, World!")
.font(.system(size: 28))
While written this way, this font does not support Dynamic Type. To address this, use the ScaledMetric property wrapper.
@ScaledMetric(relativeTo: .title) var size: CGFloat = 28
Text("Hello, World!")
.font(.system(size: size))
The value of ScaledMetric changes in response to Dynamic Type. The rate of change varies according to the style specified in relativeTo. In the example above, since 28 is set with the scaling rate of the title style, it will scale to 34 when the Dynamic Type is set to xxxLarge. For specifications on scaling rates, please refer to the HIG mentioned at the beginning.
Custom Font
When using custom fonts, an API that integrates something like ScaledMetric is provided.
Text("Hello, World!")
.font(.custom("HiraginoSans-W3", size: 28, relativeTo: .title))
I wish they would provide this for system fonts as well 🤔
Choosing Not to Support It
While supporting Dynamic Type is fundamentally beneficial, it often leads to UI breakage,
(though it's personally a bit of a shame) there may be apps that choose not to support it. Below, I will introduce several ways to handle such cases.
Styles
If you want to use styles but don't want them to respond to Dynamic Type, you can use dynamicTypeSize(_:). This API allows you to fix a View's Dynamic Type size.
Text("Hello, World!")
.font(.title)
.dynamicTypeSize(.large) // Becomes Large, ignoring user settings
Since Large is the default size for iOS/iPadOS, this effectively means the app won't support Dynamic Type.
System Font
As mentioned in the support patterns, if you use system fonts, the following method works fine 🙆♀️
Text("Hello, World!")
.font(.system(size: 28))
Custom Font
When using custom fonts, you can use custom(_:fixedSize:).
Text("Hello, World!")
.font(.custom("HiraginoSans-W3", fixedSize: 28))
(Extra) Limiting the Dynamic Type Range
For design reasons, you may want to limit the range of size variations caused by Dynamic Type. For example, a design system requirement might specify scaling up to twice the default size.
Limiting Based on Dynamic Type Sizes
In such cases, you can use dynamicTypeSize<T>(_:). We previously used the same API to fix the Dynamic Type size, but it also allows you to specify a range.
Text("Hello, World!")
.font(.title)
.dynamicTypeSize(DynamicTypeSize.large...DynamicTypeSize.xxLarge)
In this example, the text will not become smaller than Large or larger than xxLarge regardless of the Dynamic Type setting.
Limiting with Custom Sizes
Limiting based on Dynamic Type sizes does not allow for precise font size restrictions. While it's a bit of a brute-force approach, you can limit it accurately using ScaledMetric 💪
let defaultSize: CGFloat = 28
@ScaledMetric(relativeTo: .title) var scaledSize: CGFloat = 28
var size: CGFloat {
var scale = scaledSize / defaultSize
let minScale: CGFloat = 1
let maxScale: CGFloat = 1.5
scale = max(scale, minScale)
scale = min(size, maxScale)
return defaultSize * scale
}
Text("Hello, World!")
.font(.system(size: size))
In this example, by setting minimum and maximum scales, the size is restricted to a range of 28 to 42.
Summary
There is definitely a demand for Dynamic Type from users, and it is something worth supporting. In fact, there is significant demand in the apps I am involved with.
However, it is inherently difficult to handle, and the more rigorous the design specifications, the harder it becomes to support. I believe it's important to coordinate closely with designers and establish a culture that embraces this feature 😌
Discussion