๐DynamicTypeSize+ext
SwiftUI โฉ Layout โฉ Dynamic Type Sizes โฉ
import SwiftUI
extension DynamicTypeSize {
public var abbreviation: String {
switch self {
case .xSmall : return "XS"
case .small : return "S"
case .medium : return "M"
case .large : return "L"
case .xLarge : return "XL"
case .xxLarge : return "XXL"
case .xxxLarge: return "XXXL"
case .accessibility1: return "AC1"
case .accessibility2: return "AC2"
case .accessibility3: return "AC3"
case .accessibility4: return "AC4"
case .accessibility5: return "AC5"
@unknown default: return "โ"
}
}
}
/// โญ๏ธ support for `String(describing:)` and `print(_:)`
extension DynamicTypeSize: CustomStringConvertible {
public var description: String {
switch self {
case .xSmall : return ".xSmall"
case .small : return ".small"
case .medium : return ".medium"
case .large : return ".large"
case .xLarge : return ".xLarge"
case .xxLarge : return ".xxLarge"
case .xxxLarge: return ".xxxLarge"
case .accessibility1: return ".accessibility1"
case .accessibility2: return ".accessibility2"
case .accessibility3: return ".accessibility3"
case .accessibility4: return ".accessibility4"
case .accessibility5: return ".accessibility5"
@unknown default: return "unknown case"
}
}
}
/// โญ๏ธ custom string interpolation:
/// usage: `"dynamic type size: \(size)"`
extension DefaultStringInterpolation {
public mutating func appendInterpolation(_ size: DynamicTypeSize) {
appendLiteral(String(describing: size))
}
}
/// โญ๏ธ LocalizedStringKey interpolation:
/// usage: `Text("\(dynamicTypeSize)")`
/// see [StackOverflow](https://stackoverflow.com/a/64768926/5409815)
extension LocalizedStringKey.StringInterpolation {
public mutating func appendInterpolation(_ size: DynamicTypeSize) {
appendInterpolation(String(describing: size))
}
}
import SwiftUI
struct DynamicTypeSizes: View {
let sizes = DynamicTypeSize.allCases
var body: some View {
List(sizes, id: \.self){ size in
// โญ๏ธ custom string interpolation with `LocalizedStringKey`
Text("\(size)")
.environment(\.dynamicTypeSize, size) // โญ๏ธ dynamic type size
}
}
}
SwiftUI โฉ EnvironmentValues โฉ
DynamicTypeSize (enum)
Last updated