Skip to content

Instantly share code, notes, and snippets.

@hacknicity
Created January 27, 2023 10:45
Show Gist options
  • Save hacknicity/7195e1c65c9c5db53419edf6b2f19dc6 to your computer and use it in GitHub Desktop.
Save hacknicity/7195e1c65c9c5db53419edf6b2f19dc6 to your computer and use it in GitHub Desktop.
SwiftUI sample showing different system font widths, including an undocumented extra expanded width
//
// ContentView.swift
// SystemFontWidths
//
// Created by Geoff Hackworth on 27/01/2023.
//
import SwiftUI
struct ContentView: View {
private let fontWidths: [Font.Width] = [.compressed, .condensed, .standard, .expanded, .extraExpanded]
var body: some View {
VStack(spacing: 8) {
ForEach(fontWidths, id: \.self) { width in
HStack(spacing: 8) {
Text("Hello, world!")
.frame(maxWidth: .infinity, alignment: .trailing)
Text("\(width.name)")
.frame(maxWidth: .infinity, alignment: .leading)
}
.fontWidth(width)
}
}
.padding()
}
}
extension Font.Width {
/// Undocumented Font.Width
static let extraExpanded = Font.Width(0.3)
}
private extension Font.Width {
var name: String {
let names: [Font.Width: String] = [
.compressed: "compressed",
.condensed: "condensed",
.standard: "standard",
.expanded: "expanded",
.extraExpanded: "extraExpanded"
]
return names[self] ?? "unknown"
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
@hacknicity
Copy link
Author

hacknicity commented Jan 27, 2023

gist

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment