๐ดDynamic Type Sizes
Last updated
Last updated
DynamicTypeSize+ext (extension)
Human Interface Guidelines โฉ
Dynamic Type Sizes (iOS)
Dynamic Type Sizes (watchOS)
SwiftUI โฉ State and Data Flow โฉ EnvironmentValues (struct) โฉ
.dynamicTypeSize : DynamicTypeSize (enum)
.isAccessibilitySize : Bool - case accessibility1
~ accessibility5
FiveStars โฉ How to make SwiftUI views adaptive
ไพไธ๏ผ้กฏ็คบๅ็จฎ dynamic type size ็็ธๅฐๅคงๅฐใ
ไพไบ๏ผๆ นๆ dynamic type size ๅฐ HStack ่ชฟๆด็บ VStackใ
struct DynamicTypeSizeView: View {
let sizes = DynamicTypeSize.allCases
var body: some View {
List(sizes, id: \.self){ size in
// โญ๏ธ custom string interpolation with `LocalizedStringKey`
Text("\(size)")
// โญ๏ธ dynamic type size (for child views)
.environment(\.dynamicTypeSize, size)
}
}
}
struct ContentView: View {
// โญ๏ธ dynamic type size
@Environment(\.dynamicTypeSize) var typeSize: DynamicTypeSize
@State private var currentTypeSize: DynamicTypeSize = .medium
let allSizes = DynamicTypeSize.allCases
var typeSizeLargeThanXL: Bool {
currentTypeSize > .xLarge
}
var body: some View {
let _ = print("environ. type size: \(typeSize)")
let _ = print("selected type size: \(currentTypeSize)")
VStack {
// โญ๏ธ AdaptiveHStack
AdaptiveHStack(threshold: typeSizeLargeThanXL) {
Text("Text A")
Text("Text B")
Text("Text C")
}
// โญ๏ธ set dynamic type size (for child viewsโโ)
// โ ๆณจๆ๏ผ้่ฃก็่จญๅฎๆฏ็ตฆใchild viewsใ็๏ผไธๆๅพๅคๅณ้ใ
.environment(\.dynamicTypeSize, currentTypeSize)
Spacer()
// Picker
Picker("Type Size", selection: $currentTypeSize) {
ForEach(allSizes, id: \.self) { size in
Text("\(size.abbreviation)")
.tag(size)
}
}.pickerStyle(.segmented)
}
}
}