Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

@samuel-mellert samuel-mellert commented Jun 26, 2015

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

This comment has been minimized.

Copy link

@ChristopherAlan ChristopherAlan commented Jul 31, 2015

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

This comment has been minimized.

Copy link
Owner Author

@samuel-mellert samuel-mellert commented Aug 13, 2015

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