Skip to content

Instantly share code, notes, and snippets.

@macguru
Created March 8, 2024 08:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save macguru/9dc5371496db6b318303ac717bb7910c to your computer and use it in GitHub Desktop.
Save macguru/9dc5371496db6b318303ac717bb7910c to your computer and use it in GitHub Desktop.
SwiftUI view to try out SF Pro with various widths and weights
import SwiftUI
struct ContentView: View {
@State var width: Float = 0
@State var weight: Float = 0
var body: some View {
VStack {
HStack{
Text("Width")
Spacer()
Slider(value: $width, in: -0.4...0.3)
Spacer()
Text(String(format: "%.f%%", (width+1)*100))
}
HStack{
Text("Weight")
Spacer()
Slider(value: $weight, in: -0.8...0.6)
Spacer()
Text(String(format: "%.f%%", (weight+1)*100))
}
Text("Hello, world!")
.font(Font(uiFont(width: width, weight: weight)))
}
.padding()
}
#if os(macOS)
typealias XFont = NSFont
#else
typealias XFont = UIFont
#endif
func uiFont(width: Float, weight: Float) -> XFont! {
let desc = XFont
.monospacedDigitSystemFont(ofSize: 40, weight: .regular)
.fontDescriptor
.addingAttributes([.traits: [
kCTFontWidthTrait: width,
kCTFontWeightTrait: weight
]])
return XFont(descriptor: desc, size: 0)
}
}
#Preview {
ContentView()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment