Skip to content

Instantly share code, notes, and snippets.

@liorazi
Created February 11, 2018 15:19
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liorazi/94e85f36e97908716e48f0ebb0ac753a to your computer and use it in GitHub Desktop.
Save liorazi/94e85f36e97908716e48f0ebb0ac753a to your computer and use it in GitHub Desktop.
Font Extension for Monospaced Digits (Swift 4.0)
import UIKit
extension UIFont {
var monospacedDigitFont: UIFont {
let oldFontDescriptor = fontDescriptor
let newFontDescriptor = oldFontDescriptor.monospacedDigitFontDescriptor
return UIFont(descriptor: newFontDescriptor, size: 0)
}
}
private extension UIFontDescriptor {
var monospacedDigitFontDescriptor: UIFontDescriptor {
let fontDescriptorFeatureSettings = [[UIFontDescriptor.FeatureKey.featureIdentifier: kNumberSpacingType, UIFontDescriptor.FeatureKey.typeIdentifier: kMonospacedNumbersSelector]]
let fontDescriptorAttributes = [UIFontDescriptor.AttributeName.featureSettings: fontDescriptorFeatureSettings]
let fontDescriptor = self.addingAttributes(fontDescriptorAttributes)
return fontDescriptor
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment