Skip to content

Instantly share code, notes, and snippets.

@standinga
Created September 28, 2019 22:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save standinga/b5a798fea61621a799a1abbe2c1e751e to your computer and use it in GitHub Desktop.
Save standinga/b5a798fea61621a799a1abbe2c1e751e to your computer and use it in GitHub Desktop.
UIFont extension for monospaced font.
import UIKit
extension UIFont {
static func monospacedFont(at size: CGFloat) -> UIFont{
let bodyFontDescriptor = UIFontDescriptor.preferredFontDescriptor(withTextStyle: UIFont.TextStyle.body)
let bodyMonospacedNumbersFontDescriptor = bodyFontDescriptor.addingAttributes(
[
UIFontDescriptor.AttributeName.featureSettings: [
[
UIFontDescriptor.FeatureKey.featureIdentifier: kNumberSpacingType,
UIFontDescriptor.FeatureKey.typeIdentifier: kMonospacedNumbersSelector
]
]
])
return UIFont(descriptor: bodyMonospacedNumbersFontDescriptor, size: size)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment