Skip to content

Instantly share code, notes, and snippets.

@samuel-mellert
Created June 26, 2015 15:09
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 samuel-mellert/a0419641895ff4c2ba82 to your computer and use it in GitHub Desktop.
Save samuel-mellert/a0419641895ff4c2ba82 to your computer and use it in GitHub Desktop.
extension UIFont {
static func monospacedDigitsSystemFontOfSize(fontSize: CGFloat, weight: CGFloat) -> UIFont {
let originalFontDescriptor = UIFont.systemFontOfSize(fontSize, weight: weight).fontDescriptor()
let fontDescriptorFeatureSettings = [
[
UIFontFeatureTypeIdentifierKey: kNumberSpacingType,
UIFontFeatureSelectorIdentifierKey: kMonospacedNumbersSelector
]
]
let fontDescriptorAttributes = [UIFontDescriptorFeatureSettingsAttribute: fontDescriptorFeatureSettings]
let fontDescriptor = originalFontDescriptor.fontDescriptorByAddingAttributes(fontDescriptorAttributes)
return UIFont(descriptor: fontDescriptor, size: 0)
}
}
@samuel-mellert
Copy link
Author

Adds missing static method to UIFont, that generates a UIFont with monospaced numeral rendering (which is disabled by default when linking against iOS 9 SDK - there you'll get proportional spaced numeral rendering).

@ChristopherAlan
Copy link

FYI In Xcode 7 beta 4 I was able to get the same behavior as NSFont in the apple keynote
example: label.font = UIFont.monospacedDigitSystemFontOfSize(62, weight: UIFontWeightUltraLight)

@samuel-mellert
Copy link
Author

Ah, you are right. They seem to have fixed this with beta 4! Great to know. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment