Skip to content

Instantly share code, notes, and snippets.

@longvudai
Forked from shaps80/Font.swift
Created June 30, 2021 03:53
Show Gist options
  • Save longvudai/e2af9c8b0afb65f138f4971dc461a531 to your computer and use it in GitHub Desktop.
Save longvudai/e2af9c8b0afb65f138f4971dc461a531 to your computer and use it in GitHub Desktop.
A set of UIFont helpers that matches the equivalent SwiftUI Font API.
import UIKit
public extension UIFont {
enum Leading {
case loose
case tight
}
private func addingAttributes(_ attributes: [UIFontDescriptor.AttributeName: Any]) -> UIFont {
return UIFont(descriptor: fontDescriptor.addingAttributes(attributes), size: pointSize)
}
static func system(size: CGFloat, weight: UIFont.Weight, design: UIFontDescriptor.SystemDesign = .default) -> UIFont {
let descriptor = UIFont.systemFont(ofSize: size).fontDescriptor
.addingAttributes([
UIFontDescriptor.AttributeName.traits: [
UIFontDescriptor.TraitKey.weight: weight.rawValue
]
]).withDesign(design)!
return UIFont(descriptor: descriptor, size: size)
}
static func system(_ style: UIFont.TextStyle, design: UIFontDescriptor.SystemDesign = .default) -> UIFont {
let descriptor = UIFontDescriptor.preferredFontDescriptor(withTextStyle: style).withDesign(design)!
return UIFont(descriptor: descriptor, size: 0)
}
func weight(_ weight: UIFont.Weight) -> UIFont {
return addingAttributes([
UIFontDescriptor.AttributeName.traits: [
UIFontDescriptor.TraitKey.weight: weight.rawValue
]
])
}
func italic() -> UIFont {
let descriptor = fontDescriptor.withSymbolicTraits(.traitItalic)!
return UIFont(descriptor: descriptor, size: 0)
}
func bold() -> UIFont {
let descriptor = fontDescriptor.withSymbolicTraits(.traitBold)!
return UIFont(descriptor: descriptor, size: 0)
}
func leading(_ leading: Leading) -> UIFont {
let descriptor = fontDescriptor.withSymbolicTraits(leading == .loose ? .traitLooseLeading : .traitTightLeading)!
return UIFont(descriptor: descriptor, size: 0)
}
func smallCaps() -> UIFont {
return addingAttributes([
.featureSettings: [
[
UIFontDescriptor.FeatureKey.featureIdentifier: kLowerCaseType,
UIFontDescriptor.FeatureKey.typeIdentifier: kLowerCaseSmallCapsSelector
],
[
UIFontDescriptor.FeatureKey.featureIdentifier: kUpperCaseType,
UIFontDescriptor.FeatureKey.typeIdentifier: kUpperCaseSmallCapsSelector
]
]
])
}
func lowercaseSmallCaps() -> UIFont {
return addingAttributes([
.featureSettings: [
[
UIFontDescriptor.FeatureKey.featureIdentifier: kLowerCaseType,
UIFontDescriptor.FeatureKey.typeIdentifier: kLowerCaseSmallCapsSelector
]
]
])
}
func uppercaseSmallCaps() -> UIFont {
return addingAttributes([
.featureSettings: [
[
UIFontDescriptor.FeatureKey.featureIdentifier: kUpperCaseType,
UIFontDescriptor.FeatureKey.typeIdentifier: kUpperCaseSmallCapsSelector
]
]
])
}
func monospacedDigit() -> UIFont {
return addingAttributes([
.featureSettings: [
[
UIFontDescriptor.FeatureKey.featureIdentifier: kNumberSpacingType,
UIFontDescriptor.FeatureKey.typeIdentifier: kMonospacedNumbersSelector
]
]
])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment