https://github.com/frzi/swift-variablefonts
Extends UIFont/NSFont (and SwiftUI Font) for easier variable font support
https://github.com/frzi/swift-variablefonts
fonts swift swiftui variable
Last synced: 7 months ago
JSON representation
Extends UIFont/NSFont (and SwiftUI Font) for easier variable font support
- Host: GitHub
- URL: https://github.com/frzi/swift-variablefonts
- Owner: frzi
- License: mit
- Created: 2023-11-28T12:00:43.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-10-13T11:32:23.000Z (over 1 year ago)
- Last Synced: 2024-12-23T00:50:22.622Z (about 1 year ago)
- Topics: fonts, swift, swiftui, variable
- Language: Swift
- Homepage:
- Size: 18.6 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Variable Fonts
> Easier use of variable fonts with AppKit, UIKit and SwiftUI. For iOS, macOS, tvOS, watchOS and visionOS

[](https://developer.apple.com/xcode/swiftui)
[](https://swift.org)
[](https://developer.apple.com/xcode)
[](https://opensource.org/licenses/MIT)
Extends AppKit's `NSFont`, UIKit's `UIFont` and SwiftUI's `Font` with variable font features. Couldn't be easier!
## How-to-use
### Initializing font with axes.
```swift
let font = NSFont(name: "Amstelvar", size: 20, axes: [
.weight: 650,
.opticalSize: 144,
"GRAD": 500,
])
```
### New font with a single axis applied
```swift
let scienceGothic = UIFont(name: "ScienceGothic", size: 20)!
let slanted = scienceGothic.withAxis(.slant, value: -10)
```
### Get all available axes of a font
```swift
let tiltWarp = NSFont(name: "TiltWarp-Regular", size: 100)!
let axes = tiltWarp.allAxes()
print(axes)
/*
[VariableFonts.FontAxis(
id: 1481789268,
name: "XROT",
description: "Rotation in X",
minimumValue: -45.0,
maximumValue: 45.0,
defaultValue: 0.0),
etc...]
*/
```
### SwiftUI
```swift
Text("Hello world")
.font(.custom(name: "Fraunces", size: 40, axes: [
.weight: 900,
"SOFT": 100,
"WONK": 1,
]))
```
### Example: maxed out font
```swift
let nunito = UIFont(name: "NunitoSans", size: 20)!
let axes = nunito.allAxes()
// Creates a UIFont with all axes set to their maximum value.
let megaNunito = nunito.withAxes(
Dictionary(uniqueKeysWithValues: axes.map { axis in
return (axis.id, axis.maximumValue)
})
)
```
## Axis names
The dictionary you supply to configure the axes use [`FontAxis.Name`](Sources/VariableFonts/FontAxis.swift#L38) as keys. This type comes with a set of well known axis names. I.e. `.weight` (`wght`), `.width` (`wdth`), etc. This type is `ExpressibleByStringLiteral`. String literals can be used for custom axis names.