Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
]
]
])
}
}
@shaps80

This comment has been minimized.

Copy link
Owner Author

@shaps80 shaps80 commented Jul 25, 2020

Usage:

font = .system(.title3, design: .serif)
    .weight(.semibold)
    .italic()

font = .system(size: 18, weight: .medium, design: .rounded)
    .monospacedDigit()
    .smallcaps()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment