Created
March 8, 2024 08:09
-
-
Save macguru/9dc5371496db6b318303ac717bb7910c to your computer and use it in GitHub Desktop.
SwiftUI view to try out SF Pro with various widths and weights
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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